-
|
I'm coming from nestjs and jest / vitest and currently evaluate if japa is a better test runner for my adonisjs app. I came across the problem that I found no way of tempering with the env-vars before the app starts for my test. // pseudocode
it('my fancy test', () => {
const startedContainer = // start the testcontainer
process.env.SOME_VAR = startedContainer.xyz
const app = //start my nest app
expect(...) // test the behavior of the change
})Is this possible with japa and adonisjs? My current usecase is that I need to restart a container on every test run, since it does not support deleting data. But other usecases would be to test if e.g. feature-toggles work. For how to get testcontainers working in general I found https://github.com/orgs/adonisjs/discussions/4453 , but sadly this is not as useful for my case, since I want a "changing container" |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
|
I would need a bit more info to share an accurate way to mutate env variables for your use-case. If you just want to override an env variable, then you may use the In AdonisJS, the app is booted once and then all tests are executed against the same instance. Which means, the app is not re-booted for every single test or test file. What I would like to know is
|
Beta Was this translation helpful? Give feedback.
-
|
I got it working with vitest: create another file /*
|--------------------------------------------------------------------------
| Test runner entrypoint for usage with vitest
|--------------------------------------------------------------------------
|
*/
process.env.NODE_ENV = 'test';
import 'reflect-metadata';
import { Ignitor } from '@adonisjs/core';
/**
* URL to the application root. AdonisJS need it to resolve
* paths to file and directories for scaffolding commands
*/
const APP_ROOT = new URL('../', import.meta.url);
/**
* The importer is used to import files in context of the
* application.
*/
const IMPORTER = (filePath: string) => {
if (filePath.startsWith('./') || filePath.startsWith('../')) {
return import(new URL(filePath, APP_ROOT).href);
}
return import(filePath);
};
process.send = undefined; // Prevent Adonis from sending messages via process.send and confuse vitest
export const adonisApp = new Ignitor(APP_ROOT, { importer: IMPORTER }).tap(app => {
app.booting(async () => {
await import('#start/env');
});
app.listen('SIGTERM', () => app.terminate());
app.listenIf(app.managedByPm2, 'SIGINT', () => app.terminate());
});and in your vitest config: import { defineConfig } from 'vitest/config';
export default defineConfig({
test: {
environment: 'node',
pool: 'forks',
poolOptions: {
forks: {
// execArgv: ['--import', 'tsx'],
execArgv: ['--import', 'ts-node-maintained/register/esm'],
},
},
// [...]
},
});then you can write vitest files like this: import { beforeAll, describe, expect, test } from 'vitest';
import { adonisApp } from '../../../bin/test.vite.js';
import { type ApplicationService } from '@adonisjs/core/types';
import { Logger } from '@adonisjs/core/logger';
describe('adonisjs-vitest', () => {
let app: ApplicationService;
beforeAll(async () => {
await adonisApp.httpServer().start();
const appOrUndefined = adonisApp.getApp();
if (!appOrUndefined) {
throw new Error('Failed to get Adonis application instance');
}
app = appOrUndefined;
return () => adonisApp.terminate();
});
test('sample test', async () => {
const logger = await app.container.make(Logger);
expect(logger).toBeDefined();
logger.info('TestLogEntry');
});
}); |
Beta Was this translation helpful? Give feedback.
I got it working with vitest:
create another file
bin/test.vite.ts: