Skip to content
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

add jest+typescript test #155

Merged
merged 6 commits into from
Sep 20, 2022
Merged

add jest+typescript test #155

merged 6 commits into from
Sep 20, 2022

Conversation

iambumblehead
Copy link
Owner

@iambumblehead iambumblehead commented Sep 20, 2022

@cyberwombat either "tsm" or "ts-node" could be used the same way here, but this PR uses --loader=tsm now.

re #154

@iambumblehead
Copy link
Owner Author

@cyberwombat would this type of test setup work for your tests?

@cyberwombat
Copy link

I should be able to use tsm - right now I use ts-jest transform. Ive never used tsm as a loader but happy to experiment.

@iambumblehead
Copy link
Owner Author

I did not know about ts-jest transform... I'm interested to change this PR to use the ts-jest transform

@cyberwombat
Copy link

Ya my jest.config.ts looks like this:

export default {
  testEnvironment: 'jest-environment-node',
  setupFiles: ['./test/setup.ts'],
  setupFilesAfterEnv: ['./test/bootstrap.ts'],
  transform: {
    '^.+\\.tsx?$': [
      'ts-jest',
      {
        useESM: true,
        diagnostics: false,
        compilerHost: true,
        incremental: true
      }
    ]
  },
  moduleNameMapper: {
    '^(\\.{1,2}/.*)\\.js$': '$1',
    '^lib/(.*)$': 'src/lib/$1',
   ...
  },
  extensionsToTreatAsEsm: ['.ts'],
  testPathIgnorePatterns: ['<rootDir>/dist', '.history'],
  modulePaths: ['<rootDir>/src', '<rootDir>/test', '<rootDir>'],
  preset: 'ts-jest'
}

@iambumblehead
Copy link
Owner Author

Actually, based on my admittedly limited understanding of jest... probably jest transform will not work. I think the ts transformation must occur through a --loader like tsm or ts-node. I can try to use the jest.config.ts provided but I don't believe I will succeed.

@cyberwombat
Copy link

I am not attached - I do want to be able to use TS files for my setupFiles and setupFilesAfterEnv however as these reference common TS libs in main code. I will try using tsm instead tomorrow and see what happens in general.

@iambumblehead
Copy link
Owner Author

jest tries to use ts-node, so updating this PR to use ts-node rather than tsm seems like a good idea.

when using the jest.config.ts, errors like these occur...

Error: Jest: Failed to parse the TypeScript config file /home/bumble/software/esmock/tests/tests-jest-ts/jest.config.ts
  Error: Must use import to load ES Module: /home/bumble/software/esmock/tests/tests-jest-ts/jest.config.ts
require() of ES modules is not supported.
require() of /home/bumble/software/esmock/tests/tests-jest-ts/jest.config.ts from /home/bumble/software/esmock/tests/tests-jest-ts/node_modules/jest-config/build/readConfigFileAndSetRootDir.js is an ES module file as it is a .ts file whose nearest parent package.json contains "type": "module" which defines all .ts files in that package scope as ES modules.
Instead change the requiring code to use import(), or remove "type": "module" from /home/bumble/software/esmock/tests/tests-jest-ts/package.json.

@cyberwombat
Copy link

Hmm. I can't tell if its struggling with a TS config or just the ESM. I think the latter. What I did is have "type": "module" in my package.json and use this for my tsconfig.json:

{
  "extends": "@tsconfig/node14/tsconfig.json",
  "compilerOptions": {
    "useUnknownInCatchVariables": false,
    "target": "es2020",
    "lib": ["es2020"],
    "module": "esnext",
    "moduleResolution": "Node",
    "esModuleInterop": true, // Eases ESM support
    "types": ["node"],
    "allowSyntheticDefaultImports": true,
    "declaration": true,
    "strict": true,
    "strictNullChecks": true,
    "alwaysStrict": true,
    "incremental": true,
    "noImplicitReturns": false,
    "noImplicitThis": false,
    "noUnusedLocals": false,
    "noUnusedParameters": false,
    "noEmit": true,
    "noFallthroughCasesInSwitch": false,
    "inlineSourceMap": true,
    "inlineSources": true,
    "experimentalDecorators": true,
    "strictPropertyInitialization": false,
    "resolveJsonModule": true,
    "outDir": "dist",
    "baseUrl": ".",
    "paths": {
  
    },
    "typeRoots": [
      "node_modules/@types",
      "typings"
      //"node_modules/graphql/type/schema.d.ts",
    ],
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true
  }
}

@cyberwombat
Copy link

Once think I wanted to avoid is doing all imports with 'js' extensions as that makes a total mess for jest/ts-jest. Also I had to play around to have VSCode Inteliisense play nice so these settings are the result of that.

@iambumblehead
Copy link
Owner Author

I haven't succeeded in replacing tsm with ts-jest :/

@cyberwombat
Copy link

What about ts-node? From reading comments here and there it seems to have a better overall support w Jest and has native esm loader https://www.npmjs.com/package/ts-node

@iambumblehead
Copy link
Owner Author

when tsm is replaced with ts-node/esm, this happens when the tests run,

 FAIL  ./esmock.node-jest.test.ts
  ● Test suite failed to run

    thrown: Object {

        "diagnosticCodes": Array [
          2593,
          2304,
        ],
      }

@iambumblehead
Copy link
Owner Author

a ts-node/esm test folder with passing esmock tests is here https://github.com/iambumblehead/esmock/tree/master/tests/tests-nodets this error appears to occur specifically in combination with jest

@iambumblehead
Copy link
Owner Author

iambumblehead commented Sep 20, 2022

@cyberwombat the ts-node/esm test at this PR is passing. If you have time and would provide the favor of trying to add something like your jest.config.ts file to the PR that would be awesome. If we can support that file, we should include that with the test.

To use the esmock tests, from esmock's root folder npm install && npm run test:install to fetch all dependencies and then from inside tests/test-jest-ts/ run npm test

@iambumblehead
Copy link
Owner Author

let's merge this now, in order to update the wiki with links to this test-jest-ts folder. A separate PR could be used for any subsequent changes

@iambumblehead iambumblehead merged commit 3ca10f6 into master Sep 20, 2022
@iambumblehead iambumblehead deleted the add-jest-typescript-test branch September 20, 2022 18:38
@cyberwombat
Copy link

I am having no luck w jest-light-runner. I was not able to run directly from repo (too many links/globals I could not match) but have same scenario on my own code.

@iambumblehead
Copy link
Owner Author

ok thanks for helping me to this point

@cyberwombat
Copy link

Frankly the solution is probably to dump Jest. It's pretty outdated these days and they've expressed a lack of motivation in supporting ESM mocks. I'm exploring vitest to see if that has better options. It supports TS and ESM natively which is nice. Thank you for your effort

@iambumblehead
Copy link
Owner Author

I would be interested to know what you think of vitest, as there are many positive reviews

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants