Skip to content

Commit

Permalink
feat: add test in normal template
Browse files Browse the repository at this point in the history
  • Loading branch information
meixg committed Apr 28, 2022
1 parent b84aaec commit f77abf4
Show file tree
Hide file tree
Showing 11 changed files with 148 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"cSpell.words": [
"fastify"
]
}
13 changes: 13 additions & 0 deletions packages/cli/hoth-template/templates/normal/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module.exports = {
preset: 'ts-jest',
// Note resolver required only when using imports with extensions
// resolver: 'jest-ts-webcompat-resolver',
// In test environment we setup reflect-metadata
testEnvironment: './jest.environment.cjs',
// Jest does not support ESM modules well, so you will need to define mappings to CJS modules
moduleNameMapper: {
'^fastify-decorators/testing$': 'fastify-decorators/testing/index.cjs',
'^fastify-decorators/plugins$': 'fastify-decorators/plugins/index.cjs',
'^fastify-decorators$': 'fastify-decorators/index.cjs',
},
};
11 changes: 11 additions & 0 deletions packages/cli/hoth-template/templates/normal/jest.environment.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const NodeEnvironment = require('jest-environment-node');

class FastifyDecoratorsTestEnvironment extends NodeEnvironment {
setup() {
require('reflect-metadata');
this.global.Reflect = Reflect;
return super.setup();
}
}

module.exports = FastifyDecoratorsTestEnvironment;
11 changes: 5 additions & 6 deletions packages/cli/hoth-template/templates/normal/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
"esm": "^3.2.25",
"gulp": "^4.0.2",
"husky": "^4.3.8",
"jest": "26",
"jest-environment-node": "26",
"lerna": "^3.22.1",
"lint-staged": "^10.5.4",
"nodemon": "^2.0.7",
Expand All @@ -50,13 +52,10 @@
"typescript": "^4.1.3"
},
"dependencies": {
"fastify-plugin": "^3.0.0",
"nunjucks": "^3.2.3",
"point-of-view": "^4.14.0",
"@hoth/cli": "^__cliVersion__",
"fastify": "^3.19.1",
"pino": "^6.11.1",
"v8-compile-cache": "^2.2.0",
"fastify-static": "^4.2.2"
"fastify-plugin": "^3.0.0",
"nunjucks": "^3.2.3",
"point-of-view": "^4.14.0"
}
}
2 changes: 1 addition & 1 deletion packages/cli/hoth-template/templates/normal/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import nunjucks from 'nunjucks';
import path from 'path';
import type {FastifyInstance} from 'fastify';
import type {AppConfig} from '@hoth/app-autoload';
export default async function main(fastify: FastifyInstance, config: AppConfig) {
export default async function main(fastify: FastifyInstance, config: Pick<AppConfig, 'dir'>) {
await fastify.register(pointOfView, {
engine: {
nunjucks,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Controller, GET, Inject} from '@hoth/decorators';
import type {FastifyReply, FastifyRequest} from 'fastify';
import Calculator from '../../lib/calc/index.service'
import Calculator from '../../lib/calc/index.service';

@Controller('/index')
export default class AppController {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,15 @@
import type {FastifyInstance} from 'fastify';
import fp from 'fastify-plugin';

declare module 'fastify' {
interface FastifyInstance {
foo: string;
}
}

export default fp(function (fastify: FastifyInstance, opts: any, next: any) {
console.log('foo plugin options', opts);
fastify.decorate('foo', 'foo');
next();
});

Expand Down
18 changes: 18 additions & 0 deletions packages/cli/hoth-template/templates/normal/test/app.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import main from '../src/app';
import fastify from 'fastify';
import path from 'path';

describe('main', () => {
it('should init', async () => {
const instance = fastify();

// @ts-ignore
main[Symbol.for('skip-override')] = true;
instance.register(main, {
dir: path.resolve(__dirname, '../src'),
});
await instance.ready();

expect(instance.hasReplyDecorator('view')).toBe(true);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import pointOfView from 'point-of-view';
import nunjucks from 'nunjucks';
import {FastifyInstance} from 'fastify';
import {configureControllerTest} from 'fastify-decorators/testing';
import AppController from '../../../src/controller/index/index.controller';
import Calculator from '../../../src/lib/calc/index.service';
import fp from 'fastify-plugin';
import path from 'path';

describe('Controller: AppController', () => {
let instance: FastifyInstance;
const calcService = {add: jest.fn()};

beforeEach(async () => {
instance = await configureControllerTest({
controller: AppController,

// mock services
mocks: [
{
provide: Calculator,
useValue: calcService,
},
],

// mock other plugin
plugins: [
fp(async (fastify: FastifyInstance) => {
fastify.decorateRequest('$appConfig', {get: jest.fn()});
fastify.register(pointOfView, {
engine: {nunjucks},
root: path.resolve(__dirname, '../../../src/view')
});
})
]
});
});
afterEach(() => jest.restoreAllMocks());

it('get /index', async () => {
calcService.add.mockReturnValue(20);

const result = await instance.inject({
url: '/index',
method: 'GET',
});

expect(result.body).toContain('num: 20');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import {configureServiceTest} from 'fastify-decorators/testing';
import Calculator from '../../../src/lib/calc/index.service';

describe('Service: AuthService', () => {
let service: Calculator;

beforeEach(() => {
service = configureServiceTest({
service: Calculator,
});
});
afterEach(() => jest.restoreAllMocks());

it('add', async () => {

const result = service.add(1, 2);

expect(result).toBe(3);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import fastify, {FastifyInstance} from 'fastify';
import foo from '../../../src/plugin/foo/index';

describe('Plugin: foo', () => {
let instance: FastifyInstance;

beforeEach(async () => {
instance = fastify();
instance.register(foo, 'some options');
await instance.ready();
});
afterEach(() => jest.restoreAllMocks());

it('should return opt', async () => {
expect(instance.foo).toBe('foo');
});
});

0 comments on commit f77abf4

Please sign in to comment.