codesandbox-client
codesandbox-client copied to clipboard
Jest in CodeSandbox 3.0
Creating an issue to discuss future features for Jest integration in CodeSandbox...
Jest in CodeSandbox 2.5 - issue | release
- [x] Unit testing
- [x] Mock functions
- [x] Snapshot testing (read only, does not create new snapshots)
- [x] Async runner
- [x] UI - watch mode, show test fail inline in editor, run tests for single suite
Jest in CodeSandbox 3
- [ ] Snapshot saving
- [ ] Manual mocks
- [ ] Jest config
- [ ] Code Coverage
Please comment in this issue with more features that should be added to the list.
cc @CompuIves
also cc @gillchristian - interested to help with code or ideas? :)
Yeah, I just joined Discord, we can talk there :smile:
jest.config.js support would be really nice.
@alexilyaev indeed, in the Discord I mentioned I'd like to work on it but haven't found the time yet :grimacing:
Fake timers 🙏
jest.useFakeTimers is not a function
Just came across this ticket after commenting on another ticket https://github.com/CompuIves/codesandbox-client/issues/692#issuecomment-390355107. There may be issues utilizing enzyme as well.
To clarify, people will have issues running enzyme if they utilize setupTestFrameworkScriptFile like is suggested in the official enzyme docs https://github.com/airbnb/enzyme/blob/master/docs/guides/jest.md#configure-with-jest
Would have been nicer when I called jest.mock(..) instead of getting TypeError: jest.mock is not a function, I would get a Error: not implemented yet in codesandbox.io, see: https://github.com/CompuIves/codesandbox-client/issues/513
or jest.doMock ;)
I've look through half of half of the Internet to finally find this bug :D
Would have been nicer when I called
jest.mock(..)instead of gettingTypeError: jest.mock is not a function, I would get aError: not implemented yet in codesandbox.io, see: https://github.com/CompuIves/codesandbox-client/issues/513
+1 As local code works fine, the "not a function" error is a bit misleading (to me anyways), as I literally found out about CodeSandbox today (super awesome BTW!) and trying to use it to testdrive react components and jest tests. Not knowing the architecture, I just assumed that if Jest was in the dependencies correctly, I could call any of the jest.x() functions.
So I've read this thread over 3x and still not sure if this is still an known issue or not.
I continue to get the error...
jest.mock is not a function
There seems to be codesandbox docs about using Jest... but no mention of limitations. https://codesandbox.io/docs/tests
@iDVB I don't think jest.mock()/"automatic mock" is supported yet. 🤔
How do we check the version of CodeSandbox from the dashboard btw?
@dwiyatci You can check the version in any sandbox here:

@SaraVieira Thank you. :) But what I meant was: how can we tell whether it's CodeSandbox 2.5 or 3.0? :sweat_smile:
Shhhhhit, no idea haha
Ask Ives 🤔
@compuives
jest.useFakeTimers is not a function
Hi, is there any update for above fix?
@gautamarora , @gillchristian Are you guys still interested in pursuing this?
I'd be interested in having the Snapshot Saving feature if possible!
Or is it possible already?
For example, I was thinking about the potential of the Run any NPM Script feature CodeSandbox Containers, which seems to enable running Jest with the -u flag via an npm script...
@SaraVieira just confirmed my suspicions that this is only possible at the moment with Containers.
Is CodeSandbox satisfied with this? Or would you also like to be able to create and update files on non-Container sandboxes? (maybe a whitelisted list of things that can be updated, at least to start with?)
Also Manual Mocks - those would be super useful.
Workaround: At the moment, the closest thing to a workaround that I've found is to use the import * as name from '../relative/path' syntax with mock functions, but that only seems to work for local modules, not dependencies...
import { useIt } from "./use-default-export";
import * as myfunc from "./default-export-function";
test("use-default-export-function", () => {
expect(useIt()).toEqual("real");
});
test("use-default-export-function with mock", () => {
myfunc.default = jest.fn(() => "unreal");
expect(useIt()).toEqual("unreal");
});
https://codesandbox.io/s/88wlzp6q88
People who came here because this error or similar (like me)
jest.useFakeTimers is not a function
For now you can bypass this issue and use the done() callback of jest
it("uses a timer", done => {
console.log("start");
let x = 0;
setTimeout(() => {
console.log("done!");
expect(x === 0).toBe(true);
done();
}, 250);
console.log("continue");
});
is this still an issue? I'm getting the same error from 2018 on jest.mock
is this fixed yet ?
@stephanepericat i dont think so.. its since 2018
Lol just wasted so much time trying to debug it. nvm I guess 😞
Still is not jest.mock() available in CodeSandbox?
I just tried..., what a pity!
september 2020, jest.mock and jest.useFakeTimers are still bugs.
I opened a GitHub Discussions chat (#5042) on this.
Feel free to comment over there too.
Hopefully it will help further gain traction for serious consideration to add Jest mocks support, alongside this open issue.
Hit this issue today, codesandbox: https://codesandbox.io/s/crimson-framework-jddh2?file=/src/index.test.js
There was an error while evaluating the file:
jest.useFakeTimers is not a function
jest.useFakeTimers is not a function
Issue still persists. Just checked today.
jest.mock is not a function still an error.
https://codesandbox.io/s/misty-wave-ecvgf?file=/src/setupTests.js