Skip to content

Commit

Permalink
fix Babel transform to handle TS type annotations
Browse files Browse the repository at this point in the history
The `@babel/plugin-transform-modules-commonjs` transform runs before the esbuild step, which is where TS type annotations are removed.

This can cause files that contain typings to fail with errors like:

```
SyntaxError: xxx.ts: Cannot transform the imported binding "XXX" since it's also used in a type annotation. Please strip type annotations using @babel/preset-typescript or @babel/preset-flow.
```

This commit ensures that files that are transformed via this Babel plugin have their type annotations removed via the `@babel/preset-typescript` preset beforehand.

Fixes aelbore#57
  • Loading branch information
petebacondarwin committed Jan 5, 2022
1 parent daa5847 commit 61e3f62
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 9 deletions.
8 changes: 4 additions & 4 deletions examples/names-ts/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import { expect } from "@jest/globals"

import { display } from './index'

test('should parse with [jest.mock]', () => {
expect(display()).toEqual([ 'Joe' ])
})

jest.mock('./index', () => {
return {
display() {
return [ 'Joe' ]
}
}
})

test('should parse with [jest.mock]', () => {
expect(display()).toEqual([ 'Joe' ])
})
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
"esbuild": ">=0.9.3"
},
"devDependencies": {
"@babel/preset-typescript": "^7.13.0",
"@swc/core": "^1.2.50",
"@types/jest": "^26.0.21",
"@types/mock-fs": "^4.13.0",
Expand All @@ -51,6 +50,7 @@
"dependencies": {
"@babel/core": "^7.13.10",
"@babel/plugin-transform-modules-commonjs": "^7.13.8",
"@babel/preset-typescript": "^7.13.0",
"babel-jest": "^26.6.3"
},
"babel": {
Expand Down
1 change: 1 addition & 0 deletions src/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Config } from '@jest/types'
import babelJest from 'babel-jest'

const { process } = babelJest.createTransformer({
presets: [ "@babel/preset-typescript" ],
plugins: [ "@babel/plugin-transform-modules-commonjs" ],
parserOpts: {
plugins: ["jsx", "typescript"],
Expand Down
8 changes: 4 additions & 4 deletions tests/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ test('should have sourcemap with [jest.mock]', () => {
(0, _globals.expect)((0, _index.display)()).toEqual([\\"Joe\\"]);
});
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImluZGV4LnNwZWMudHMiXSwibWFwcGluZ3MiOiI7QUFJQSxjQUFLLEtBQUssV0FBVztBQUNuQixTQUFPO0lBQ0w7QUFDRSxhQUFPLENBQUU7Ozs7QUFQZixJQUFBLFdBQUEsUUFBQTtBQUVBLElBQUEsU0FBQSxRQUFBOzs7Ozs7OztBQVVBLEtBQUssaUNBQWlDO0FBQ3BDLEVBQUEsSUFBQSxTQUFBLFFBQU8sSUFBQSxPQUFBLFlBQVcsUUFBUSxDQUFFOzsiLCJuYW1lcyI6W10sInNvdXJjZXNDb250ZW50IjpudWxsfQ=="
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImluZGV4LnNwZWMudHMiXSwibWFwcGluZ3MiOiI7QUFRQSxjQUFLLEtBQUssV0FBVyxNQUFNO0FBQ3pCLFNBQU87SUFDTCxVQUFVO0FBQ1IsYUFBTyxDQUFFOzs7O0FBWGYsSUFBQSxXQUFBLFFBQUE7QUFFQSxJQUFBLFNBQUEsUUFBQTs7Ozs7Ozs7QUFFQSxLQUFLLGlDQUFpQyxNQUFNO0FBQzFDLEVBQUEsSUFBQSxTQUFBLFFBQU8sSUFBQSxPQUFBLFlBQVcsUUFBUSxDQUFFOzsiLCJuYW1lcyI6W10sInNvdXJjZXNDb250ZW50IjpudWxsfQ=="
`)

expect(output.map).toEqual( {
version: 3,
sources: [ 'index.spec.ts' ],
mappings: ';AAIA,cAAK,KAAK,WAAW;AACnB,SAAO;IACL;AACE,aAAO,CAAE;;;;AAPf,IAAA,WAAA,QAAA;AAEA,IAAA,SAAA,QAAA;;;;;;;;AAUA,KAAK,iCAAiC;AACpC,EAAA,IAAA,SAAA,QAAO,IAAA,OAAA,YAAW,QAAQ,CAAE;;',
mappings: ';AAQA,cAAK,KAAK,WAAW,MAAM;AACzB,SAAO;IACL,UAAU;AACR,aAAO,CAAE;;;;AAXf,IAAA,WAAA,QAAA;AAEA,IAAA,SAAA,QAAA;;;;;;;;AAEA,KAAK,iCAAiC,MAAM;AAC1C,EAAA,IAAA,SAAA,QAAO,IAAA,OAAA,YAAW,QAAQ,CAAE;;',
names: [],
sourcesContent: null
})
Expand All @@ -79,13 +79,13 @@ test('should have sourcemap without [jest.mock]', () => {
display
};
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4vZXhhbXBsZXMvbmFtZXMtdHMvaW5kZXgudHMiXSwibWFwcGluZ3MiOiJBQUFBO0FBRU87QUFDTCxTQUFPO0FBQUE7IiwibmFtZXMiOltdLCJzb3VyY2VzQ29udGVudCI6bnVsbH0="
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4vZXhhbXBsZXMvbmFtZXMtdHMvaW5kZXgudHMiXSwibWFwcGluZ3MiOiJBQUFBO0FBRU8sbUJBQW1CO0FBQ3hCLFNBQU87QUFBQTsiLCJuYW1lcyI6W10sInNvdXJjZXNDb250ZW50IjpudWxsfQ=="
`)

expect(output.map).toEqual({
version: 3,
sources: [ './examples/names-ts/index.ts' ],
mappings: 'AAAA;AAEO;AACL,SAAO;AAAA;',
mappings: 'AAAA;AAEO,mBAAmB;AACxB,SAAO;AAAA;',
names: [],
sourcesContent: null
})
Expand Down

0 comments on commit 61e3f62

Please sign in to comment.