In the eternal quest for sepation of concerns, I would like to rewrite my jest tests and move out all the data used for testing into a separate file. Example:
describe('something', () => {
it.each`
key | value
${'foo'} | ${{
'this': ['is', 'a'],
'long': ['js', 'object']
}}
${'bar'} | ${{
'i': ['don\'t', 'want'],
'all': ['this', 'stuff'],
'in': ['my test file']
}}
`("should work as expected for '$key'", async ({key, value}) => {
// Test logic goes here
actual = doSomething(key)
actual.shouldEqual(value)
})
})
Instead, I would rather prefer to write something like this:
describe('something', () => {
it.each('test/something-data.json', "should work as expected for '$key1'", async ({key1, key2}) => {
// Test logic goes here
actual = doSomething(key)
actual.shouldEqual(value)
})
})
And then would declare all my fixtures in a separate file test/something-data.json
.
Is there any way to achieve at the moment in jest? Of course, I could read the fixtures manually from a file but this would not allow me to see single successes or failures for each fixture. Or would this be a future feature request for jest?
You can achieve it, you just need to require
the json file
e.g.
it.each(
require("./test/something-data.json")
)("should work as expected for '%s'",
({ key1, key2 }) => {
// Test logic goes here
expect(doSomething(key)).toEqual(value);
}
);