[Bug]: Mixed ESM/CJS Typescript files in node_modules
Creative-Difficulty opened this issue · 1 comments
Creative-Difficulty commented
Version
^29.1.1
Steps to reproduce
My config (MOSTLY works with TypeScript and ES6):
import type { JestConfigWithTsJest } from 'ts-jest'
const jestConfig: JestConfigWithTsJest = {
preset: "ts-jest/presets/default-esm",
resolver: "ts-jest-resolver",
testEnvironment: "node",
// transformIgnorePatterns: ["<rootDir>/node_modules/"],
extensionsToTreatAsEsm: [".ts"],
collectCoverageFrom: [
"src/**/*.ts"
],
transform: {
// '^.+\\.[tj]sx?$' to process js/ts with `ts-jest`
// '^.+\\.m?[tj]sx?$' to process js/ts/mjs/mts with `ts-jest`
'^.+\\.tsx?$': [
'ts-jest',
{
useESM: true,
tsconfig: "tsconfig.json"
},
],
},
}
export default jestConfig
```
I am importing a node module into a file, on which i'm exporting a function, which i am using in my test.
The Error:
```bash
ELIFECYCLE Test failed. See above for more details.
alexeschanz@192 Oxygen % TS_JEST_LOG=ts-jest.log pnpm run test
> oxygen@0.8.1 test /Users/alexeschanz/Documents/Projects/Oxygen
> NODE_OPTIONS=--experimental-vm-modules pnpm exec jest
(node:65630) ExperimentalWarning: VM Modules is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
PASS tests/checkEnv.test.ts
ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From tests/getUser.test.ts.
ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From tests/getUser.test.ts.
FAIL tests/getUser.test.ts
● Test suite failed to run
Must use import to load ES Module: /Users/alexeschanz/Documents/Projects/Oxygen/node_modules/.pnpm/@josephg+resolvable@1.0.1/node_modules/@josephg/resolvable/index.ts
at Runtime.requireModule (node_modules/.pnpm/jest-runtime@29.7.0/node_modules/jest-runtime/build/index.js:850:21)
ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From tests/getUser.test.ts.
ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From tests/getUser.test.ts.
ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From tests/getUser.test.ts.
ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From tests/getUser.test.ts.
ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From tests/getUser.test.ts.
ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From tests/getUser.test.ts.
ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From tests/getUser.test.ts.
ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From tests/getUser.test.ts.
ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From tests/getUser.test.ts.
ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From tests/getUser.test.ts.
(node:65631) ExperimentalWarning: VM Modules is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
Test Suites: 1 failed, 1 passed, 2 total
Tests: 2 passed, 2 total
Snapshots: 0 total
Time: 1.641 s, estimated 2 s
Ran all test suites.
ELIFECYCLE Test failed. See above for more details.
```
This is the test itself:
```ts
import { getUser } from "../src/lib/getUser.js";
import { User, UserKey } from "../src/types.js";
import { testDB } from "../src/lib/db.js";
import { v4 as uuidv4 } from "uuid";
import * as fs from "node:fs/promises";
import jsonwebtoken from "jsonwebtoken";
import path from "node:path";
import bcrypt from "bcryptjs";
import { JWTSecret } from "../src/index.js";
// jest.useFakeTimers();
const dirname = path.resolve();
const testDBDir = path.join(dirname, "test-databse")
const currentDateTime = new Date().toISOString();
const randomInvalidJWT = jsonwebtoken.sign({ created: currentDateTime, uuid: uuidv4() }, uuidv4())
test("getUser Filters out JWT's that don't start with Bearer", async () => {
const getUserResult = await getUser(randomInvalidJWT);
console.log(getUserResult);
expect(getUserResult).toBe(false);
});
test("getUser Doesn't filter out JWT's that start with space", async () => {
const userUUID: string = uuidv4();
const currentDateTime = new Date().toISOString();
const userJWT = jsonwebtoken.sign({ created: currentDateTime, uuid: userUUID }, JWTSecret);
const encryptedJWTToken = await bcrypt.hash(userJWT, 15);
const keyObject: UserKey = { uuid: userUUID, created: currentDateTime, access: false };
const valueObject = {
JWT: encryptedJWTToken,
uuid: userUUID
} as User
await testDB.put(JSON.stringify(keyObject), JSON.stringify(valueObject));
const getUserResult = await getUser(` Bearer ${userJWT}`);
expect(getUserResult).toBe(true);
});
afterAll(async () => {
await fs.rm(testDBDir, { recursive: true });
})
```
### Expected behavior
The tests to work
### Actual behavior
The tests didnt work
### Debug log
https://justpaste.it/bttkp
### Additional context
_No response_
### Environment
```shell
System:
OS: macOS 14.0
CPU: (10) arm64 Apple M2 Pro
Binaries:
Node: 20.7.0 - ~/.nvm/versions/node/v20.7.0/bin/node
npm: 10.1.0 - ~/.nvm/versions/node/v20.7.0/bin/npm
pnpm: 8.8.0 - /opt/homebrew/bin/pnpm
npmPackages:
jest: ^29.7.0 => 29.7.0
```
Creative-Difficulty commented
MIgrated to bun. LOsers