-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(es/jest): Handle
@jest/globals
(#8994)
**Description:** - We have two `jest` pass. One in `@swc/core` (via _hidden.jest flag), and one in the plugin. This PR fixes only the core one. However, the linked issue is wrong because the code tries to break the rules of ESM specification. So, although this PR closes the issue, the final form is not the issue the author wanted. **Related issue:** - Closes swc-project/plugins#310
- Loading branch information
Showing
5 changed files
with
131 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"$schema": "https://json.schemastore.org/swcrc", | ||
"jsc": { | ||
"parser": { | ||
"syntax": "typescript", | ||
"tsx": true, | ||
"dynamicImport": true | ||
}, | ||
"transform": { | ||
"react": { | ||
"runtime": "automatic" | ||
} | ||
}, | ||
"baseUrl": "./", | ||
"target": "es2021", | ||
"keepClassNames": true | ||
}, | ||
"isModule": true, | ||
"module": { | ||
"type": "commonjs", | ||
"ignoreDynamic": false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
|
||
import { setTimeout } from 'timers/promises'; | ||
|
||
import { describe, expect, it, jest } from '@jest/globals'; | ||
|
||
jest.mock('timers/promises', () => ({ | ||
setTimeout: jest.fn(() => Promise.resolve()) | ||
})); | ||
|
||
describe('suite', () => { | ||
it('my-test', () => { | ||
const totalDelay = jest | ||
.mocked(setTimeout) | ||
.mock.calls.reduce((agg, call) => agg + (call[0] as number), 0); | ||
expect(totalDelay).toStrictEqual(0); | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
const _promises = require("node:timers/promises"); | ||
const _globals = require("@jest/globals"); | ||
_globals.jest.mock('timers/promises', ()=>({ | ||
setTimeout: _globals.jest.fn(()=>Promise.resolve()) | ||
})); | ||
(0, _globals.describe)('suite', ()=>{ | ||
(0, _globals.it)('my-test', ()=>{ | ||
const totalDelay = _globals.jest.mocked(_promises.setTimeout).mock.calls.reduce((agg, call)=>agg + call[0], 0); | ||
(0, _globals.expect)(totalDelay).toStrictEqual(0); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters