In-source testing
With in-source testing, we can indicate to Vitest to look into our JavaScript and Single Component Files for the tests to run, as opposed to specific files. These alternatives are not exclusive to each other, so we could have both active at the same time. The reason behind this is that, in some cases, a test case would benefit to be “close” to the original code that it is trying to assert. Such code must be placed at the end of our file following this format:
if (import.meta.vitest) { const { describe, test, expect } = import.meta.vitest //... Test functions here }
Then, for Vitest to find this code in our files, we also need to modify the vite.config.js
file to include the following:
export default defineConfig({ test: { includeSource: ['src/**/*.{js,ts}'], // other configurations...