refactor(middleware-joi): integration with
MODIFY
packages/middleware-joi/package.json
packages/middleware-joi/package.json
},
"devDependencies": {
"@marblejs/core": "^3.2.0",
- "@marblejs/middleware-body": "^3.2.0"
+ "@marblejs/middleware-body": "^3.2.0",
+ "@marblejs/testing": "3.2.0"
},
"publishConfig": {
"access": "public"

MODIFY
packages/middleware-joi/test/integration/integration.spec.ts
packages/middleware-joi/test/integration/integration.spec.ts
-import * as request from 'supertest';
-import { createServer } from '@marblejs/core';
-import { createHttpServerTestBed } from '@marblejs/core/dist/+internal/testing';
+import { createHttpTestBed, createTestBedSetup } from '@marblejs/testing';
+import { pipe } from 'fp-ts/lib/pipeable';
import { listener } from '../helpers/api.spec-util';
+const testBed = createHttpTestBed({ listener });
+const useTestBedSetup = createTestBedSetup({ testBed });
+
describe('Joi middleware - Integration', () => {
const token = '181782881DB38D84';
- const server = createServer({ listener });
- const httpTestBed = createHttpServerTestBed(server);
+ const testBedSetup = useTestBedSetup();
beforeEach(() => {
jest.spyOn(console, 'warn').mockImplementation(jest.fn);
});
- test('should fail without a token', async () => {
- const expected = {
+ afterEach(async () => {
+ await testBedSetup.cleanup();
+ });
+
+ test('fails without a token', async () => {
+ const { request } = await testBedSetup.useTestBed();
+
+ const response = await pipe(
+ request('GET'),
+ request.withPath('/api/user/1'),
+ request.send,
+ );
+
+ expect(response.statusCode).toEqual(400);
+ expect(response.body).toEqual({
error: {
status: 400,
- message: '"token" is required'
+ message: '"token" is required',
}
- };
-
- return request(httpTestBed.getInstance())
- .get('/api/user/1')
- .then(res => expect(res.body).toEqual(expected));
});
+ });
+
+ test('sends GET request with parameters', async () => {
+ const { request } = await testBedSetup.useTestBed();
- test('should send a get request with parameters', async () => {
- return request(httpTestBed.getInstance())
- .get('/api/user/1')
- .set('token', token)
- .expect(200, { id: 1 });
+ const response = await pipe(
+ request('GET'),
+ request.withPath('/api/user/1'),
+ request.withHeaders({ 'token': token }),
+ request.send,
+ );
+
+ expect(response.statusCode).toEqual(200);
+ expect(response.body).toEqual({ id: 1 });
});
- test('should send a get request with an invalid param', async () => {
- const expected = {
+ test('sends GET request with an invalid param', async () => {
+ const { request } = await testBedSetup.useTestBed();
+
+ const response = await pipe(
+ request('GET'),
+ request.withPath('/api/user/11'),
+ request.withHeaders({ 'token': token }),
+ request.send,
+ );
+
+ expect(response.statusCode).toEqual(400);
+ expect(response.body).toEqual({
error: {
status: 400,
- message: '"id" must be less than or equal to 10'
+ message: '"id" must be less than or equal to 10',
}
- };
-
- return request(httpTestBed.getInstance())
- .get('/api/user/11')
- .set('token', token)
- .then(res => expect(res.body).toEqual(expected));
});
+ });
+
+ test('sends GET request with query', async () => {
+ const { request } = await testBedSetup.useTestBed();
+
+ const response = await pipe(
+ request('GET'),
+ request.withPath('/api/post?page=2'),
+ request.withHeaders({ 'token': token }),
+ request.send,
+ );
- test('should send a get request with query', async () => {
- return request(httpTestBed.getInstance())
- .get('/api/post?page=2')
- .set('token', token)
- .expect(200, { page: 2 });
+ expect(response.statusCode).toEqual(200);
+ expect(response.body).toEqual({ page: 2 });
});
- test('should send a post request with body', async () => {
- return request(httpTestBed.getInstance())
- .post('/api/user')
- .set('token', token)
- .send({ name: 'lucio' })
- .expect(200, { name: 'lucio', passport: 'marble.js' });
+ test('sends POST request with body', async () => {
+ const { request } = await testBedSetup.useTestBed();
+
+ const response = await pipe(
+ request('POST'),
+ request.withPath('/api/user'),
+ request.withHeaders({ 'token': token }),
+ request.withBody({ name: 'lucio', passport: 'marble.js' }),
+ request.send,
+ );
+
+ expect(response.statusCode).toEqual(200);
+ expect(response.body).toEqual({ name: 'lucio', passport: 'marble.js' });
});
- test('should send a post request with query and body', async () => {
+ test('sends POST request with query and body', async () => {
const time = Date.now();
- return request(httpTestBed.getInstance())
- .post(`/api/post?timestamp=${time}`)
- .set('token', token)
- .send({ title: 'Middleware Joi' })
- .expect(200, { title: 'Middleware Joi', timestamp: new Date(time).toISOString() });
+ const { request } = await testBedSetup.useTestBed();
+
+ const response = await pipe(
+ request('POST'),
+ request.withPath(`/api/post?timestamp=${time}`),
+ request.withHeaders({ 'token': token }),
+ request.withBody({ title: 'Middleware Joi' }),
+ request.send,
+ );
+
+ expect(response.statusCode).toEqual(200);
+ expect(response.body).toEqual({ title: 'Middleware Joi', timestamp: new Date(time).toISOString() });
});
});

refactor(middleware-joi): integration with @marblejs/testing