最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to create Jest Custom Environment with Typescript? - Stack Overflow

programmeradmin2浏览0评论

I am trying to create an extension off jest-node-environment as a CustomTestEnvironment but am getting the following error when trying to run jest

● Test suite failed to run

    ~/git/my-application/tests/environment/custom-test-environment.ts:1
    import NodeEnvironment from 'jest-environment-node';
    ^^^^^^

    SyntaxError: Cannot use import statement outside a module

      at runTestInternal (../node_modules/jest-runner/build/runTest.js:226:5)

I believe this error means it doesn't recognize this as a typescript file, and hasn't transpiled it. ( I am using the latest version of jest 26.0.1)

Based on discussions in the jest github, the PR to make this work was slated for Jest 26 but was pulled from Jest 26 and is (hopefully) going to be in Jest 27.

That being said, I've seen samples of people doing it this way online but for me I'm not having any luck following their lead.

import NodeEnvironment from "jest-environment-node";
import {LocalObject} from "./object/local-object.helper";

export class CustomTestEnvironment extends NodeEnvironment {

    public async setup(): Promise<void> {
        await super.setup();
        this.global.localObject = LocalObject.init()
    }

    public async teardown(): Promise<void> {
        LocalObject.teardown(this.global.localObject)
        await super.teardown();
    }
}

The LocalObject is just a thin wrapper around a test utility which has a plex startup and teardown and which I want to provide to tests to be able to publish test data and kick off the ponent test.

If I change the imports to be require -

const NodeEnvironment = require("jest-environment-node");
const {LocalObject} =  require("./object/local-object.helper");

Then I get this error instead -

SyntaxError: Unexpected token 'export'

If I move the export into an module.exports then I get the following error

public async setup(): Promise<void> {
       ^^^^^              

SyntaxError: Unexpected token 'async'

Seems to me like it's not treating this file as typescript.

Is there any workaround to be able to use this as a typescript file? The LocalObject is written in typescript, so I believe that I need this to be typescript to work right with that one, and it is important that LocalObject file remains typescript for test files to use it correctly.

Alternative question: Can I do the same kind of setup/teardown logic in the setupFilesAfterEnv I only saw that they were run before, but not after tests. Thanks.

I am trying to create an extension off jest-node-environment as a CustomTestEnvironment but am getting the following error when trying to run jest

● Test suite failed to run

    ~/git/my-application/tests/environment/custom-test-environment.ts:1
    import NodeEnvironment from 'jest-environment-node';
    ^^^^^^

    SyntaxError: Cannot use import statement outside a module

      at runTestInternal (../node_modules/jest-runner/build/runTest.js:226:5)

I believe this error means it doesn't recognize this as a typescript file, and hasn't transpiled it. ( I am using the latest version of jest 26.0.1)

Based on discussions in the jest github, the PR to make this work was slated for Jest 26 but was pulled from Jest 26 and is (hopefully) going to be in Jest 27. https://github./facebook/jest/pull/8751

That being said, I've seen samples of people doing it this way online but for me I'm not having any luck following their lead.

import NodeEnvironment from "jest-environment-node";
import {LocalObject} from "./object/local-object.helper";

export class CustomTestEnvironment extends NodeEnvironment {

    public async setup(): Promise<void> {
        await super.setup();
        this.global.localObject = LocalObject.init()
    }

    public async teardown(): Promise<void> {
        LocalObject.teardown(this.global.localObject)
        await super.teardown();
    }
}

The LocalObject is just a thin wrapper around a test utility which has a plex startup and teardown and which I want to provide to tests to be able to publish test data and kick off the ponent test.

If I change the imports to be require -

const NodeEnvironment = require("jest-environment-node");
const {LocalObject} =  require("./object/local-object.helper");

Then I get this error instead -

SyntaxError: Unexpected token 'export'

If I move the export into an module.exports then I get the following error

public async setup(): Promise<void> {
       ^^^^^              

SyntaxError: Unexpected token 'async'

Seems to me like it's not treating this file as typescript.

Is there any workaround to be able to use this as a typescript file? The LocalObject is written in typescript, so I believe that I need this to be typescript to work right with that one, and it is important that LocalObject file remains typescript for test files to use it correctly.

Alternative question: Can I do the same kind of setup/teardown logic in the setupFilesAfterEnv I only saw that they were run before, but not after tests. Thanks.

Share Improve this question edited May 24, 2020 at 1:40 GoldFlsh asked May 23, 2020 at 15:04 GoldFlshGoldFlsh 1,3344 gold badges14 silver badges29 bronze badges 2
  • 2 Have you managed to solve this yet? I'm running into the same issue right now. – ddominnik Commented Oct 18, 2020 at 16:27
  • No I had to use setupFilesAfterEnv instead. (see answer) – GoldFlsh Commented Oct 29, 2020 at 16:04
Add a ment  | 

1 Answer 1

Reset to default 3

UPDATE: Jest 27 is now released and now supports this. If you are using an older version of Jest, update to Jest 27 to be able to use CustomEnvironment as typescript https://jestjs.io/blog/2021/05/25/jest-27#features-ing-with-breaking-changes

Modules used in the following configuration options are now transformed like the rest of your code, which may be breaking if you relied on them being loaded as-is:

  • testEnvironment
  • runner
  • testRunner
  • snapshotResolver

Alternatively, you can continue to use the workaround below for prior versions.


This isn't supported in Jest 26 and is slated to be in Jest 27 https://github./facebook/jest/pull/8751#issuement-699049851

For now the solution is to use setupFilesAfterEnv files in my jest.config https://jestjs.io/docs/en/configuration#setupfilesafterenv-array.

From there I could put all my my setup/teardown in beforeAll() and afterAll() blocks. This was effectively equivalent to using the Node Environment and the setupFilesAfterEnv files is patible with typescript.

//jest.setup.ts
import {LocalObject} from "./object/local-object.helper";

let localObject: LocalObject;

beforeAll(async () => {
  //Start my environment or seed data to DB or whatever
  localObject = await LocalObject.init()
}


afterAll(async () => {
  //teardown or clean things started in setup my environment
  await localObject.teardown()
}
发布评论

评论列表(0)

  1. 暂无评论