refactor: use jest test environment
See original GitHub issueI’d be happy to refactor the way this module is invoked to use the Jest custom environment feature, which I think would be a more intuitive way of setting up tests.
For example, at the moment the setupTest approach breaks it.only for example, which might be non-intuitive to a person who doesn’t understand what setupTest is doing under the hood. (Alternatively, the solution is being clearer in the documentation.)
However, because this would be a significant change, I wanted to get a 👍 from @pi0 and @ricardogobbosouza first. Thoughts?
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:10 (6 by maintainers)
Top Results From Across the Web
6 stages of refactoring a Jest test case - Snyk
An underrated feature of Jest is customizing the way assertion errors that the console displays when tests fail are handled.
Read more >Refactor: Use parameterised testing using `jest.it.each` · Issue #506 ...
Currently config , tools and coverage tests use jest.it.each . The same can be done for utils , extensions and install tests.
Read more >Configuring Jest
Jest attempts to scan your dependency tree once (up-front) and cache it in order to ease some of the filesystem churn that needs...
Read more >Definitive Guide to Unit Testing in React Applications with Jest ...
Rendering component trees in a simple test environment and making ... time it would take to update the unit tests makes serious refactoring...
Read more >Unit testing with Prisma
The singleton file tells Jest to mock a default export (the Prisma client instantiated in ./client.ts ), and uses the mockDeep method from...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Thanks for quick reply. I was not sure what should be the next step, but now everything got clear. I will work on the real implementation and will send a PR. Most probably that will not be very fast. I made a plan on how to implement everything. Let’s see.
(TypeScript and tests – these will be included for sure.)
First of all, thank you for this library. After trying it out, I though it would work brilliantly as Jest environment. Happy to find this discussion.
Working Prototype
To try out quickly how
@nuxt/test-utilsenvironment could work I created this working prototype.That is just a quickly written code. I was trying to make only minimal changes by wrapping Jest environment module around current API. Of course, this is clumsy, but both implementations work without conflicting.
Before diving into code or details I wanted to see the whole picture. What problems the code should solve? So I was concentrating on:
These parts are the most important in the repo.
Proposal
The test environment could improve several things:
simplify setup. Test environment declaration lets us know the exact structure of a project.
nuxt-apppointsoptions.fixturetoprocess.cwd().nuxt-modulelooks for fixture(s) insidetest/fixtureor custom directories (current default);.onlyworks as expected;slightly better performance (see Benchmark bellow).
Examples
Test environment can be declared by adding a
@jest-environmentdocblock at the top of a test file or it can be defined through ajest.config.*file.nuxt-appnuxt-moduleFor more details see README inside the repo.
Benchmark
As I mentioned, the test environment is just an additional wrapper of current core implementation. Current
@nuxt/test-utilscode base was changed only minimally.Each suite was ran ten times in similar conditions: one warm up run, headless browser, no linting. Here are the average run times:
JavaScript example
setupTest, 25.77sTypeScript example
setupTest, 42.85sRobots Module
setupTest, 73.71sJest environment proofs to be ca. 10-20% faster. Both implementations are slow enough. That’s the nature of end-to-end tests.
Final Thoughts
I would be more than happy to work on this project. It was fun to research and to scratch the surface of Jest. Feels tempting to dive deeper.
Before moving on I would like to ask:
test-utils?Thanks for reading this longish text!