-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
148 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"cSpell.words": [ | ||
"fastify" | ||
] | ||
} |
13 changes: 13 additions & 0 deletions
13
packages/cli/hoth-template/templates/normal/jest.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
11
packages/cli/hoth-template/templates/normal/jest.environment.cjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/cli/hoth-template/templates/normal/src/controller/index/index.controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
packages/cli/hoth-template/templates/normal/test/app.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
50 changes: 50 additions & 0 deletions
50
packages/cli/hoth-template/templates/normal/test/controller/index/index.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); |
20 changes: 20 additions & 0 deletions
20
packages/cli/hoth-template/templates/normal/test/lib/calc/index.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
17 changes: 17 additions & 0 deletions
17
packages/cli/hoth-template/templates/normal/test/plugin/foo/index.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); |