Open jacob-8 opened 2 years ago
Wallaby doesn't currently support inline tests in source files using if (import.meta.vitest) {
.
Adding as a feature request.
Adding as a feature request.
Thanks!
Wallaby doesn't currently support inline tests in source files using
if (import.meta.vitest) {
.
Things work great however if I rename foo.ts
to foo.test.ts
, but that's kind of annoying (and breaking convention) to be naming files with helper functions using the .test
addition. I've tried figuring things out from Vitest and Wallaby's docs myself but am not having luck. Do you have any tips for me on how to make Wallaby scan all my typescript files so I don't have to do crazy renaming hacks? I tried adding the following to my package.json with no luck:
"wallaby": {
"tests": [
"src/**/*.ts"
]
}
I think in combo with Smart Start this would work very well.
Things work great however if I rename foo.ts to foo.test.ts
At this point in time, we're not entirely sure how vitest
works under the covers to run tests where you us if (import.meta.vitest) {
so while renaming your files may work right now, there's no guarantee that this will continue to work in the future.
One approach you may like to try is to try overriding the files
and tests
configuration for your project (see our docs). You will need to make sure that any file with if (import.meta.vitest) {
is explicitly excluded from files patterns and is explicitly included in tests patterns. You could start by hard-coding foo.ts
and see if it works for you.
Also expressing my interest in this feature!
Many of the tests where inline tests are most useful are for small chunks of code where creating a separate test file is burdensome. These are also often the tests that wallaby is most handy for too.
Folks using vitest that want to use inline tests alongside regular tests, this wallaby.js
config will do the trick:
import path from 'node:path'
import fm from 'file-matcher'
export default async () => {
const root = 'src/'
const ext = '{js,jsx,ts,tsx}'
const filePattern = '**/*.' + ext
const testFilePattern = '**/*.{spec,test}.' + ext
const inlineTestPattern = filePattern
const fileOptions = {
path: root,
recursiveSearch: true,
fileFilter: {
fileNamePattern: testFilePattern,
}
}
const inlineOptions = {
path: root,
recursiveSearch: true,
fileFilter: {
fileNamePattern: inlineTestPattern,
content: /import\.meta\.vitest/,
}
}
const cwd = process.cwd()
const relative = filename => path.relative(cwd, filename)
const fileMatcher = new fm.FileMatcher()
const fileTests = (await fileMatcher.find(fileOptions)).map(relative)
const inlineTests = (await fileMatcher.find(inlineOptions)).map(relative)
return {
autoDetect: true,
files: [
root + filePattern,
...fileTests.map(file => `!${file}`),
...inlineTests.map(file => `!${file}`)
],
tests: [
...fileTests,
...inlineTests,
],
}
}
@stagas - thanks for sharing your configuration.
While this may partially work for some projects, it can also lead to errors. For example, if you have a file that imports from your includeSource
mixed source/test file that is later tested by a separate test file, you will receive a Wallaby error similar to:
Test file 'src/my.spec.js' is importing or is causing import of another test file 'inlineTests/myFile.js'.
This may cause tests in the 'inlineTests/myFile.js' file to be executed more than once (when the test file runs and every time it gets imported).
If the 'inlineTests/myFile.js' test file contains some test helper code, try refactoring the reused code into a file of its own and use it by importing the new file into 'src/my.spec.js' and 'inlineTests/myFile.js' test files.
You will also find that Wallaby App no longer includes your inline test files in your project code coverage calculations.
If this works for you, please go ahead and use your configuration, but we wanted to highlight that there are some larger fundamental Wallaby changes that are required to properly support this scenario and this configuration does not add proper support for vitest
inline testing.
Issue description or question
In my monorepo, I have a
packages/parts/src/lib/foo.ts
file which contains a function and an inline test:My
vite.config.js
points Vitest to run this test and it does, successfully:Wallaby doesn't find this test. However, if I change my filename to
foo.test.ts
if runs just fine. Am I missing something in the documentation? Wallaby has no trouble with running an inline test when the extension istest.ts
, but rather doesn't act on theincludeSource
property of the vitest config to run tests found in files pointed to byincludeSource
.Wallaby diagnostics report