Skip to content
This repository has been archived by the owner on Jan 16, 2025. It is now read-only.

Mocking 'crypto' not working #100

Closed
joshlartz opened this issue May 23, 2022 · 6 comments
Closed

Mocking 'crypto' not working #100

joshlartz opened this issue May 23, 2022 · 6 comments

Comments

@joshlartz
Copy link

When using @swc/jest, mocks against crypto are not working, but they do work with ts-jest.

Versions:
"@swc/core": "^1.2.186",
"@swc/jest": "^0.2.21",
"jest": "^27.5.1",
"typescript": "^4.6.4",
"ts-jest": "^27.1.5"

// crypto.ts
import { randomUUID } from 'crypto';

export const cryptoTest = () => randomUUID();


// crypto.test.ts
import { cryptoTest } from './crypto.ts';
import * as crypto from 'crypto';

jest.spyOn(crypto, 'randomUUID').mockImplementation(() => '123456');

test('should be mocked', () => expect(cryptoTest()).toEqual('123456'));

Results in:

Expected: "123456"
Received: "75852814-ebca-4080-92da-3f9db02aebf6"

@kdy1
Copy link
Member

kdy1 commented May 23, 2022

Please fill all input fields

@kdy1 kdy1 closed this as completed May 23, 2022
@swc-project swc-project locked and limited conversation to collaborators May 23, 2022
@swc-project swc-project unlocked this conversation May 23, 2022
@kdy1
Copy link
Member

kdy1 commented May 23, 2022

Oh sorry I thought it was swc main repo

@kdy1
Copy link
Member

kdy1 commented May 23, 2022

I think this is duplciate, though

@joshlartz
Copy link
Author

I think this is duplciate, though

I'm sorry, a duplicate of which issue? #14 didn't seem quite the same as I don't get an error; the mocking just doesn't happen.

@kdy1
Copy link
Member

kdy1 commented May 23, 2022

@joshlartz
Copy link
Author

Thank you. You're saying this is actually working correctly and ts-jest has always done it wrong? swc-project/swc#3843 (comment)

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

No branches or pull requests

2 participants