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

Typescript support #1

Closed
kirillgroshkov opened this issue Apr 3, 2022 · 15 comments
Closed

Typescript support #1

kirillgroshkov opened this issue Apr 3, 2022 · 15 comments

Comments

@kirillgroshkov
Copy link

Feature request: would be awesome if it supported typescript compilation (no type-checking needed).

Maybe something simple like esbuild transformer can be added for *.ts files, that'll cover 99% of the common needs.

Awesome runner, by the way!

@cspotcode
Copy link

I bet you can use ts-node's ESM loader to do this today. Turn on swc mode for speed.

@cspotcode
Copy link

I made a demo combining ts-node with this test runner:
https://github.com/TypeStrong/ts-node-repros/tree/jest-light-runner/

I see that esmock is mentioned in the readme. If you want to combine ts-node with esmock or any other loader, try this:
https://www.npmjs.com/package/@cspotcode/multiloader
Or wait for node to release loader chaining support.

@kirillgroshkov
Copy link
Author

@cspotcode awesome, it works!

I'll close the issue then! 👍

@cspotcode
Copy link

Any chance this can be added to the README?

@nicolo-ribaudo
Copy link
Owner

Thank you @cspotcode! I'll create an "examples" folder that shows both this and how to mock ES modules using a loader.

@iambumblehead
Copy link

iambumblehead commented Sep 20, 2022

from this PR and discussion iambumblehead/esmock#155 we found that, using the latest node v18.9.0...

jest + jest-light-runner + ts-node/esm do not work are working, cc @cspotcode
{
  "extends": "ts-node/node16/tsconfig.json",
  "ts-node": {
    "transpileOnly": true,
    "files": true
  },
  "compilerOptions": {
    "allowSyntheticDefaultImports": true,
    "module": "ESNext",
    "moduleResolution": "node"
  }
}
{
  "type": "module",
  "description": "esmock unit tests, jest with ts and jest-light-runner",
  "dependencies": {
    "ts-node": "^10.9.1",
    "jest": "^29.0.3",
    "jest-light-runner": "0.4.0",
    "run-script-os": "^1.1.6"
  },
  "jest": {
    "runner": "jest-light-runner"
  },
  "scripts": {
    "test:default": "NODE_OPTIONS=\"--loader=ts-node/esm\" jest test.ts",
    "test:win32": "set NODE_OPTIONS=\"--loader=ts-node/esm\" && jest test.ts",
    "test": "run-script-os"
  }
}
 PASS  ./test.ts
  ✓ should pass a test when using jest

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        1.101 s
Ran all test suites matching /test.ts/i
jest + jest-light-runner + tsm are working
{
  "type": "module",
  "description": "esmock unit tests, jest with ts and jest-light-runner",
  "dependencies": {
    "tsm": "^2.2.2",
    "jest": "^29.0.3",
    "jest-light-runner": "0.4.0",
    "run-script-os": "^1.1.6"
  },
  "jest": {
    "runner": "jest-light-runner"
  },
  "scripts": {
    "test:default": "NODE_OPTIONS=\"--loader=tsm\" jest test.ts",
    "test:win32": "set NODE_OPTIONS=\"--loader=tsm\" && jest test.ts",
    "test": "run-script-os"
  }
}
 PASS  ./test.ts
  ✓ should pass a test when using jest

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        0.792 s
Ran all test suites matching /test.ts/i

@cspotcode
Copy link

Looks like you do not want typechecking, and you got a typechecking error here. So I would turn off typechecking. transpileOnly should do the trick.

@iambumblehead
Copy link

@cspotcode thank you, with this tsconfig.json, the ts-node test is passing. I will update the comment above shortly,

{
  "extends": "ts-node/node16/tsconfig.json",
  "ts-node": {
    "transpileOnly": true,
    "files": true
  },
  "compilerOptions": {
    "allowSyntheticDefaultImports": true,
    "module": "ESNext",
    "moduleResolution": "node"
  }
}

@cspotcode
Copy link

Sounds good, you can also remove the files option since that one only affects typechecking. With transpileOnly enabled, it has no effect.

@iambumblehead
Copy link

iambumblehead commented Sep 20, 2022

the person who opened the issue at esmock, @cyberwombat, also wants to use a jest.config.ts file that 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'
}

Is it possible to support this, while using jest-light-runner with --loader=ts-node/esm?

@iambumblehead
Copy link

ts-node fails in windows CI pipeline, seemingly because of the slash "/" in "ts-node/esm"

{
  "scripts": {
    "test:win32": "set NODE_OPTIONS=\"--loader=ts-node/esm --loader=esmock\" && jest esmock.node-jest.test.ts"
  }
}

https://github.com/iambumblehead/esmock/actions/runs/3091643663/jobs/5001967546

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './esm --loader=esmock' is not defined by "exports" in D:\a\esmock\esmock\tests\tests-jest-ts\node_modules\ts-node\package.json imported from D:\a\esmock\esmock\tests\tests-jest-ts\
    at new NodeError (node:internal/errors:393:5)
    at throwExportsNotFound (node:internal/modules/esm/resolve:340:9)

@cspotcode
Copy link

Mistake in your quoting

Package subpath './esm --loader=esmock' is not defined by "exports"

It thinks the subpath is this full string:

esm --loader=esmock

Cuz of a mistake in quoting

@iambumblehead
Copy link

it seems the solution is so obvious, no explanation is needed :) but I do not know the solution. I think I understand the problem you are pointing out, but I don't know how to solve it

@liuxingbaoyu
Copy link
Contributor

https://stackoverflow.com/questions/535975/dealing-with-quotes-in-windows-batch-scripts

Does this work?
"test:win32": "set \"NODE_OPTIONS=--loader=ts-node/esm --loader=esmock\" && jest esmock.node-jest.test.ts"

@iambumblehead
Copy link

iambumblehead commented Sep 20, 2022

the suggestion will be used in the test pipeline here https://github.com/iambumblehead/esmock/actions/runs/3091925933/jobs/5002604159 @liuxingbaoyu it worked! I was not able to solve that --thank you for that suggestion

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

No branches or pull requests

5 participants