Closed DiamondYuan closed 1 year ago
Thanks for the sample repo.
Wallaby's behavior for your application is actually expected. In your tests, you are using getInstance()
from tests/utils.ts
where you bypass Vitest's pipeline for processing files with a custom callout to esbuild.buildSync
and then subsequently when you run the generated code with vm.runInContext
.
This may work for Vitest's code coverage mechanism with istanbul
but you will see that it fails the same way as in the Wallaby when you use the default v8
code coverage provider (see coverage report using v8 below):
% Coverage report from v8
--------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
--------------|---------|----------|---------|---------|-------------------
All files | 86.11 | 90.9 | 100 | 86.11 |
Stepper | 82.14 | 90 | 100 | 82.14 |
utils.ts | 82.14 | 90 | 100 | 82.14 | 47-56
_util | 100 | 100 | 100 | 100 |
fmtEvent.ts | 100 | 100 | 100 | 100 |
--------------|---------|----------|---------|---------|-------------------
Unfortunately we don't have a way of supporting your current method of loading your code files.
I'm wondering what's the reason for doing this? If you are able to structure your code to load the files without a customer transpiler / and vm
context, then Wallaby will start reporting coverage.
Thanks for the sample repo.
Wallaby's behavior for your application is actually expected. In your tests, you are using
getInstance()
fromtests/utils.ts
where you bypass Vitest's pipeline for processing files with a custom callout toesbuild.buildSync
and then subsequently when you run the generated code withvm.runInContext
.This may work for Vitest's code coverage mechanism with
istanbul
but you will see that it fails the same way as in the Wallaby when you use the defaultv8
code coverage provider (see coverage report using v8 below):% Coverage report from v8 --------------|---------|----------|---------|---------|------------------- File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s --------------|---------|----------|---------|---------|------------------- All files | 86.11 | 90.9 | 100 | 86.11 | Stepper | 82.14 | 90 | 100 | 82.14 | utils.ts | 82.14 | 90 | 100 | 82.14 | 47-56 _util | 100 | 100 | 100 | 100 | fmtEvent.ts | 100 | 100 | 100 | 100 | --------------|---------|----------|---------|---------|-------------------
Unfortunately we don't have a way of supporting your current method of loading your code files.
I'm wondering what's the reason for doing this? If you are able to structure your code to load the files without a customer transpiler / and
vm
context, then Wallaby will start reporting coverage.
Components depend on global my
and Componet
, that's why I'm executing the code in vm.
Is there any way to expose coverage data to wallaby.js manually?
Is there any way to expose coverage data to wallaby.js manually?
Wallaby's code coverage is proprietary and there is currently no way to push code coverage results.
Having said that, what you're doing can be done natively from within the testing framework, and I think it would be better to lean into what is natively supported by the testing framework vs. creating your own bespoke mechanism to transform and collect code coverage.
You will need to modify your tests a little, but you can do something like this:
1. Change tests/utils.ts
to stub the globals you need
tests/utils.ts
...
// Add below other imports
import { vi } from 'vitest';
...
// Change getInstance() method to:
async function getInstance(
name: string,
props: Record<string, any>,
api?: Record<string, any>
) {
let result;
vi.stubGlobal('my', api);
vi.stubGlobal('Component', (obj) => {
result = createInstance(obj, props, api, {});
});
await import(path.join(__dirname, `../src/${name}/index.ts`))
return result;
}
This code now uses the vitest
stubGlobal
method for your my
and Component
globals.
2. Update your tests to use getInstance() as async instead of synchronous call
src/Badge/tests/index.test.ts
import { getInstance } from '../../../tests/utils';
import { describe, it, expect } from 'vitest';
describe('badge overCount', () => {
it('badge overCount true', async () => {
const instance = await getInstance('Badge', {
text: 102,
});
expect(instance.getData().overCount).toBe(true);
});
it('badge overCount false', async () => {
const instance = await getInstance('Badge', {
text: 75,
});
expect(instance.getData().overCount).toBe(false);
});
});
Issue description or question
REPO: https://github.com/ant-design/ant-design-mini/tree/vm
Here is the process I tested
I can get the test coverage correctly with
vitest run --coverage
, but not with wallaby.js.Expected
Open
src/Badge/__tests__/index.test.ts
, run wallaby , and get the coverage insrc/Badge/index.ts
.Wallaby diagnostics report