A cross-functional and emotionally intelligent executive leader and Army veteran with over 25 years of proven expertise in driving software engineering and product excellence.
I'm not sure how much I like this. I think a lot but maybe a little. Perhaps my one issue is that a render test is the least amount of testing. Snapshot tests for each screen would add an acceptable level of coverage.
And that’s exactly the problem we want to avoid—creating a test for each page when the content is static and completely different from each other is impractical. How would you test over 1000 pages like this?
A cross-functional and emotionally intelligent executive leader and Army veteran with over 25 years of proven expertise in driving software engineering and product excellence.
But wouldn't this mean you'd have a huge snapshot, namely the whole DOM for a page? Every time you change something, you'd need to look into the test and then update it.
A cross-functional and emotionally intelligent executive leader and Army veteran with over 25 years of proven expertise in driving software engineering and product excellence.
You're right about that. You can configure Jest to generate separate snapshot files by creating what Jest calls a "snapshot resolver."
const path = require('path');
module.exports = {
/**
* Saves snapshots in a __snapshots__ folder near each component file
* @param testPath
* @param snapshotExtension
* @returns
*/
resolveSnapshotPath: (testPath, snapshotExtension) => {
const dir = path.dirname(testPath);
return path.join(dir, '__snapshots__', `${path.basename(testPath)}${snapshotExtension}`);
},
resolveTestPath: (snapshotPath, snapshotExtension) => {
return snapshotPath.slice(0, -snapshotExtension.length);
},
/**
* This is a little confusing, but this is the path to the test file that
* Jest will use to check for consistency between `resolveSnapshotPath` and
* `resolveTestPath`. It just needs to point to a real test file.
* @type {string}
*/
testPathForConsistencyCheck: 'some/example.test.js',
};
Then you tell Jest to use your snapshot resolver in your Jest config file (jest.config.js):
Snapshots shouldn't be used to test. These only tell you that something rendered without a probable problem, but it doesn't convey any information about something. For example, it wouldn't tell you if a hero image is rendered on the top or in the middle, which I think you would like to test.
A cross-functional and emotionally intelligent executive leader and Army veteran with over 25 years of proven expertise in driving software engineering and product excellence.
You probably read that somewhere or heard it from a more senior dev, but it's wrong. Snapshots should absolutely be used to test. They just shouldn't be your only test, as they test at one of the highest possible scopes for a component, but it is the best way to test render, which is a unit that needs to be tested. Regardless, snapshot testing will at least provide better coverage than him simply checking if the component renders without error since he can't feasibly do specific unit tests.
A cross-functional and emotionally intelligent executive leader and Army veteran with over 25 years of proven expertise in driving software engineering and product excellence.
👋 I'm a Frontend enthusiast & full-stack aficionado | Crafting captivating user experiences | Based in Lisbon, Portugal 🇵🇹 | Dedicated to innovation and staying ahead with the latest in tech!
I'm not sure how much I like this. I think a lot but maybe a little. Perhaps my one issue is that a render test is the least amount of testing. Snapshot tests for each screen would add an acceptable level of coverage.
And that’s exactly the problem we want to avoid—creating a test for each page when the content is static and completely different from each other is impractical. How would you test over 1000 pages like this?
Assuming you're using Jest, adding "snapshot testing" is trivial, but will provide much better coverage. Something like:
This will take a "snapshot" of the the render output, and ensure it doesn't unexpectedly change.
But wouldn't this mean you'd have a huge snapshot, namely the whole DOM for a page? Every time you change something, you'd need to look into the test and then update it.
You're right about that. You can configure Jest to generate separate snapshot files by creating what Jest calls a "snapshot resolver."
Then you tell Jest to use your snapshot resolver in your Jest config file (
jest.config.js):And updating all of those snapshots is as easy as running
jest -u.This looks useful!!!
Great post!
Snapshots shouldn't be used to test. These only tell you that something rendered without a probable problem, but it doesn't convey any information about something. For example, it wouldn't tell you if a hero image is rendered on the top or in the middle, which I think you would like to test.
You probably read that somewhere or heard it from a more senior dev, but it's wrong. Snapshots should absolutely be used to test. They just shouldn't be your only test, as they test at one of the highest possible scopes for a component, but it is the best way to test
render, which is a unit that needs to be tested. Regardless, snapshot testing will at least provide better coverage than him simply checking if the component renders without error since he can't feasibly do specific unit tests.How did you get so many likes and saves on your first post? 😂
Ok interesting, have to check it out!