-
Notifications
You must be signed in to change notification settings - Fork 36
Mocking not working #14
Comments
Same. https://github.com/kulshekhar/ts-jest/blob/master/src/transformers/hoist-jest.ts |
for us mocking stopped working once we added |
Our workaround was to declare |
@gabberr, could you provide an example? I've tried it, but I still get the same error. |
Hope this issue will be resolved soon. I try to switch from |
@diego-aquino here: Create a file 'src/mocks/bunny.ts': const mockedBunny = jest.fn()
jest.mock(
'actual/bunny/file',
() => mockedBunny
)
export default mockedBunny Inside the test where you want to mock Bunny: import 'src/mocks/bunny'
// OR, if you want to set mock implementation in the test
import bunnyMock from 'mocks/bunny'
bunnyMock.mockImplementation(() => 'my mocked bunny')
|
Try with {
"jsc": {
"transform": {
"hidden": {
"jest": true
}
}
}
} I just did and it seems to be working |
Update: setting At the moment of my message, the latest version of |
im actually still running into this, any work around? |
@th3fallen have you tried #14 (comment) ? @kdy1 The mentioned fix that closes this ticket was actually reverted, should this be reopened? |
@gabberr it was reverted indeed in swc-jest, but a commit was added to swc itself to fix the issue: see #44 (comment) Can you try with an up to date |
Ah, I forgot it. |
There should be a test for jest's mocking mechanism: #46 Can you try updating your versions of both swc and swc-jest to see if it is still broken for you? (And also can you send a way to replicate the bug? So that we can add more tests) |
It still doesn't work if you want to mock implementation and pass mock function as the desired export for the module that you're trying to mock. See: https://github.com/MhMadHamster/swc-jest-mock-bug/tree/master import { callFnOne } from "./index";
const mockedImpl = jest.fn();
jest.mock("./to-be-mocked", () => ({
fnOne: (...args) => mockedImpl(...args),
}));
describe("", () => {
it("fnOne test", () => {
callFnOne()
expect(mockedImpl).toBeCalledTimes(1);
});
}); |
I forked your repo here: https://github.com/Ayc0/swc-jest-mock-bug if you want to give it a try. I think this is related to: https://jestjs.io/docs/es6-class-mocks#calling-jestmock-with-the-module-factory-parameter In the fork, you can see that I disabled swc and here are my results without swc: There seems to be a difference with the hoisting of The |
I think the exception with swc should match the exception without swc. |
I agree, I'll open another issue |
I opened #59 for this issue specifically. I think we can close this one then |
in my case the mocking failure is from this...
|
I discovered that i had this issue because I was using |
I think But this is a guess. |
Is this still an issue? |
|
I know - |
I am a little bit confused now. |
You used the syntax for ESM, so it should follow semantics of ESM |
EDIT: the example in this comment actually passes, see below I am having trouble overriding an import in a file that is being tested, I'm unsure if this issue is related... // items/valid-items.ts
export const validItems = ["item1", "item2"] // item-validator.ts
import { validItems } from './items/valid-items'
export const isValidItem = (item: string) => {
return validItems.includes(item)
} // item-validator.test.ts
import { isValidItem } from './item-validator'
import { jest, test, expect } from '@jest/globals'
jest.mock('./items/valid-items', () => ({
validItems: ["item3", "item4"]
})
test("Items are valid", () => {
const isValid = isValidItem("item3")
// test fails
expect(isValid).toBe(true)
}) edit: I guess it apparently it is, since @gabberr 's fix above worked. thought i was losing my mind. |
I'm also running into this same issue. I'm trying to use |
I see the issue is closed, was this fixed? In what version? |
@Wazbat do you have a reproduction of your error? |
@Ayc0 I created this repo: https://github.com/pm0u/swc-jest-mock-issue The simple issue I highlighted passes, so excuse that example. I dug a bit deeper and it seems (for my case) to be related to use of Regex. You can try edit: I also added an example like @gabberr 's above - you can try that with |
There seems to indeed be an issue with the mocking with CJS: if we build in ESM, it hoist properly the But not with CJS (it gets added after the requires) (REPL): But the test works again if we remove the imports from We should create another issue as this seems to be only related to |
are you sure it is not this swc-project/swc#5205? |
In your case @pm0u, you can just drop the imports on
|
You don't seem to be running ESM code in this example (the source is ESM, but not the compiled code). Also to my knowledge, Jest isn't compatible with mocks with ESM |
but he is using esm syntax, and swc treats it strictly as esm |
This issue with @jest/globals seems to be related to swc-project/plugins#310, which indeeds points back to swc-project/swc#5205 |
The use case I have is this is a script that is ultimately run using I just want to be able to write a test in typescript and have predictable behavior with the environment it is run in, I have no idea what that above command actually compiles to. Dropping the |
If you want to keep import { jest, test, expect } from "@jest/globals";
jest.mock("./items", () => ({
items: ["baz", "qux"],
}));
test("ItemsRegex", async () => {
const { itemsRegex } = await import("./items-regex");
expect(itemsRegex).toStrictEqual(/(baz|qux)/g);
}); As SWC is respecting the fact that imports are hoisted, this is the other only option to be able to have jest.mock guaranteed to be called before the other imports |
I see, thanks for the explanation and solution! this is obviously a complex problem and I appreciate everyone taking the time to sort it out correctly and in line with established standards. I think we will be able to work with the provided solutions. thanks again. |
In my case, import from |
I switched to Vitest after I couldn't get jest working. |
Just to add. The above solutions worked for me (using the factory). The thing is that the variables in that context retain their original values so if you need to access their "mocked versions" you will need to use
Idk if it will help anyone but it took me a while to figure it out. |
I am still getting this on the latest version of SWC, removing the ject globals import seemed to do the trick |
Also simply weirdly editing it to remove only certain imports works i.e.:
Is ok but
Is not |
When using
@swc/jest
as described in the readme for transforms in Jest tests, I can't seem to use any mocking.With a test like this:
I will get:
Moving the
jest.mock()
call to the top of the test file also doesn't work.Any advice appreciated.
The text was updated successfully, but these errors were encountered: