-
Notifications
You must be signed in to change notification settings - Fork 455
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Custom Environments with TypeScript #1445
Comments
hi, this issue doesn't belong to I would suggest you to open the issue for |
tag @SimenB has |
It will be supported in Jest 26: jestjs/jest#8751 |
This is fantastic, thanks for pointing this out! |
I don't know what did you mean it will be supported. I am using jest 26.6.1 but still getting this error FAIL tests/topup-package/topup-package.spec.ts
● Test suite failed to run
/home/fsevenm/nodejs-projects/deliverit/tests/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:223:5)
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 0.025 s // test-environment.ts
import NodeEnvironment from "jest-environment-node";
import { createUser, destroyUsers } from "./utils";
class TestEnvironment extends NodeEnvironment {
async setup(): Promise<void> {
await super.setup();
const { token } = await createUser();
this.global.authToken = token;
}
async teardown(): Promise<void> {
await destroyUsers();
await super.teardown();
}
}
export default TestEnvironment; // jest.config.js
module.exports = {
preset: "ts-jest",
testEnvironment: "<rootDir>/tests/test-environment.ts",
moduleNameMapper: {
"@src/(.*)$": "<rootDir>/src/$1",
},
testPathIgnorePatterns: ["/node_modules/", "<rootDir>/build/"],
globalTeardown: "<rootDir>/tests/teardown.ts",
}; |
it didn't land in 26, but it will be in v27 (you can test it by installing |
This doesn't work. as it starts throwing |
Because |
If Jest doesn’t pass it to ts-jest, ts-jest cannot compile. I’d suggest you to debug and check first if Jest does correctly. |
@ahnpnl Okay let me check with |
Here is my // jest.config.ts
import type { InitialOptionsTsJest } from "ts-jest/dist/types";
const config: InitialOptionsTsJest = {
// testEnvironment: "./tests/my-environment.ts",
preset: "ts-jest",
testPathIgnorePatterns: ["dist"],
globalSetup: "./tests/setup.ts",
globalTeardown: "./tests/global.teardown.ts",
testTimeout: 30000,
globals: {
"ts-jest": {},
},
};
export default config; The above code works fine. Here is my import NodeEnvironment from "jest-environment-node";
import { closeDbConnection, initDbConnection } from "../src/db/dbConnector";
import request from "supertest";
import app from "../src/app/app";
const idToken =<my-token>
class CustomEnvironment extends NodeEnvironment {
async setup(): Promise<void> {
await initDbConnection(() => {
throw new Error("Database connection error, please try again later");
}, null);
const response = await request(app).get(`/api/v1/login/exchangeToken?idToken=${idToken}`);
process.env.cookie = response.headers["set-cookie"];
return super.setup();
}
async teardown() {
await closeDbConnection();
await super.teardown();
}
}
module.exports = CustomEnvironment; This error links to my code in |
I could see the
One note is: you need to set TypeScript
Looking at your files, I don't see anything strange, not sure why it doesn't work for your case. You can check out my repo example here https://github.com/ahnpnl/ts-jest-only-example |
I ran into the same problem. I noticed that |
I have checked the troubleshooting guide, it does not solve this issue.
Issue :
When creating a custom environment in TypeScript, Jest throws an error complaining about the TypeScript syntax. It appears that the custom environment is not being compiled.
Expected behavior :
Debug log:
log file content
N/A
jest.config.js
Minimal repo
https://github.com/stangerjm/ts-jest-custom-env
The text was updated successfully, but these errors were encountered: