Set of helpers to simplify the process of writing integration tests for the tramvai app. Implies full-fledged run of the app with build made by @tramvai/cli
and use of helpers for test result of requests to the server and test render of the app in the browser.
The
@tramvai/cli
package is required and should be installed manually
npm i --save-dev @tramvai/test-integration
For testing requests to the tramvai app libraries superagent and node-html-parser are used under hood.
Call of app.request
sends requests to the app. All features of superagent
are available.
Call of app.render
resolves to the HTML render that is returned from server while serving the request.
import { startCli } from '@tramvai/test-integration';
beforeAll(async () => {
app = await startCli('bootstrap', {
env: {
SOME_ENV: 'test',
},
});
}, 80000);
afterAll(() => {
return app.close();
});
it('request to main page should return status 200', async () => {
return app.request('/').expect(200);
});
it('main page HTML snapshot', async () => {
const parsed = await app.render('/');
const applicationInnerHtml = parsed.application;
expect(parsed.application).toMatchInlineSnapshot();
});
You may use another library @tramvai/test-pw to implement testing in the browser.
In order to use mocker there should be added @tramvai/module-mocker
to the tramvai app modules list.
After thar mocker will read file based mocks as it described in the docs to the mocker itself and it can be used dynamically in the tests:
it('should work with mocker', async () => {
await app.mocker.addMocks('CONFIG_API', {
'GET /test/': {
status: 200,
payload: {
status: 'OK',
response: 'smth',
},
},
});
await app.request('/api/').expect(200);
await app.papi.clearCache();
await app.mocker.removeMocks('CONFIG_API', ['GET /test/']);
await app.request('/api/').expect(500);
});
All papi methods can be used in tests, for example:
import { startCli } from '@tramvai/test-integration';
it('environment list', async () => {
const { papi } = await startCli('papi-example');
const response = await papi.apiList();
});
Available methods are:
- clearCache;
- bundleInfo;
- apiList;
Also, you can use publicPapi
(passes to ${serverUrl}/${appName}/papi/
) and privatePapi
(passes to ${serverUrl}/${appName}/private/papi/
) properties to work with your own papi methods:
import { startCli } from '@tramvai/test-integration';
it('custom method', async () => {
const { papi } = await startCli('papi-example');
const response = await papi.publicPapi('whatever');
});