Has anyone been able to use Vitest along with Cocos Creator?
I compiled a custom ESM build of the engine and set up Vitest. However, as soon as I add any *.spec.ts
file to the assets directory, I get the following error: Error: We currently do not support the package 'imports' field.
Is there any way to exclude spec files from the game build?
I already tried adding “assets/src/**/*.spec.ts” to the exclude section of tsconfig, but it didn’t help.
Any advice would be appreciated. Thanks in advance!
I can keep the spec files outside of assets directory but I would really like to them close to the source *.ts files.
Well, I found a very hacky way to do it in the preview build.
Basically, you need to mock the vitest
import. But I wonder if there’s a better way to configure SystemJS imports.
<script type="text/javascript">
const systemJSPrototype = System.constructor.prototype;
const originalResolve = systemJSPrototype.resolve;
systemJSPrototype.resolve = function (specifier, importer) {
// Handle vitest imports by returning a mock module with test functions
if (specifier === 'vitest' || specifier.startsWith('vitest/')) {
return `data:text/javascript,System.register([],function(e){
return {
execute: function(){
var mockFn = function() { return mockFn; };
var noop = function() {};
var vitestMock = {
describe: noop,
it: noop,
test: noop,
expect: function() {
return {
toBe: noop,
toEqual: noop,
toMatch: noop,
toContain: noop,
toBeDefined: noop,
toBeTruthy: noop,
toBeFalsy: noop
};
},
vi: {
fn: function() { return mockFn; },
mock: mockFn,
spyOn: mockFn
},
beforeEach: noop,
afterEach: noop,
beforeAll: noop,
afterAll: noop
};
e("default", vitestMock);
e(vitestMock);
}
}
})`;
}
return originalResolve.apply(this, arguments);
};
</script>