[jest] Add an example of testing using jest
See original GitHub issueHello,
could you add some example of testing a component that uses Routify (or its part like url
)?
This would improve our experience and avoid struggling with configuration in the future.
For example, I built my project from scratch using npx @roxi/routify@next create my-r3-app
, then installed necessary packages (npm i --save-dev jest babel-jest svelte-jester @testing-library/svelte babel
).
However, still encounter an error - referring to ESM support - like:
ReferenceError: module is not defined in ES module scope
This file is being treated as an ES module because it has a '.js' file extension
and '/home/.../svelte/my-r3-app/package.json' contains "type": "module".
To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
version 3. But also related to the 2
files babel.config.js:
module.exports = {
presets: [
[
"@babel/preset-env",
{
targets: {
node: "current"
}
}
]
],
plugins: ["@babel/plugin-transform-modules-commonjs"]
};
jest.config.js:
module.exports = {
transform: {
"^.+\\.svelte$": [
"svelte-jester",
{
"preprocess": true
}
],
"^.+\\.ts$": "ts-jest",
"^.+\\.js$": "babel-jest"
},
moduleFileExtensions: [
"js",
"ts",
"svelte"
]
};
Note: see discussion at the Discord #general
channel.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:2
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Getting Started - Jest
You just successfully wrote your first test using Jest! This test used expect and toBe to test that two values were exactly identical....
Read more >Jest Tutorial - JavaScript Unit Testing Using Jest Framework
In this Jest tutorial, we will learn about various Jest features, Jest matchers and how to use Jest framework for JavaScript Unit Testing....
Read more >Jest Tutorial for Beginners: Getting Started With JavaScript ...
Jest is a JavaScript test runner, that is, a JavaScript library for creating, running, and structuring tests. Jest ships as an NPM package,...
Read more >Jest Testing Tutorial: 5 Easy Steps - Testim Blog
1. Install Jest Globally · 2. Create a Sample Project · 3. Add Jest to the Project · 4. Write Your First Test...
Read more >How to add Unit Testing to Express using Jest - Fek.io
The testing framework I prefer to use is Jest, but it is not a requirement for unit testing an express application. Jest is...
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 the solution @cooperwalbrun . I might include a link to your post. 🙂
Alright, I did some tinkering with mocking in Jest and I managed to come up with a solution that allows for a little more flexibility in mocked values (thanks @jakobrosenberg for pointing me to your custom framework; it helped guide me to the answer).
Below demonstrates how to mock specific Routify helper functions using Jest such that rendering components which depend on them causes no errors. @jakobrosenberg I am not sure whether you would want to include this example somewhere in the Routify documentation?
Additional Findings
Interestingly, when I tried doing this:
I received internal Svelte errors in my tests. I had expected this
derived
usage to work the same asbecause the API documentation suggests that these are simply different ways to express the same thing, but that does not appear to be the case. Anyway, this is not an issue in practice because the latter approach works fine - I just wanted to call it out here for awareness.