Skip to content

Commit

Permalink
refactor: refactor test cases to remove duplication
Browse files Browse the repository at this point in the history
re #139
  • Loading branch information
kdhttps committed Jan 12, 2021
1 parent 8783b41 commit 3ef9db3
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 41 deletions.
7 changes: 1 addition & 6 deletions test/app-factory.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,7 @@ describe('connect-flash middleware', () => {

it('should be called once as app.use arg', () => {
const flash = require('connect-flash')
const app = appFactoryRewire.__get__('app')
const AppFactory = appFactoryRewire.__get__('AppFactory')
const appUseSpy = sinon.spy(app, 'use')
const appInstance = new AppFactory()

appInstance.createApp()
const appUseSpy = spyOnAppUse()

assertCalledWithFunctionAsArg(appUseSpy, flash())
sinon.restore()
Expand Down
15 changes: 4 additions & 11 deletions test/features/steps/enpoint-metric-steps.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
const { Given, When, Then, BeforeStep, AfterStep } = require('@cucumber/cucumber')
const chai = require('chai')
const assert = chai.assert
const chaiHttp = require('chai-http')
chai.use(chaiHttp)
const { setupServer } = require('../../helper')

let app
const assert = chai.assert
let requester

BeforeStep(async () => {
app = require('../../../server/app')
await app.on('appStarted', () => {
console.log('app started...')
})
requester = chai.request(app).keepOpen()
await app.rateLimiter.resetKey('::ffff:127.0.0.1')
requester = await setupServer()
})

AfterStep(async () => {
requester.close()
await requester.close()
})

Given('passport server is up and running', async () => {
Expand Down
12 changes: 3 additions & 9 deletions test/features/steps/rate-limiting-steps.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
const { Given, When, Then, AfterStep, BeforeStep } = require('@cucumber/cucumber')
const chai = require('chai')
const config = require('config')
const { setupServer } = require('../../helper')

const assert = chai.assert
const chaiHttp = require('chai-http')
chai.use(chaiHttp)
let app
let requester
require('events').defaultMaxListeners = 100

BeforeStep(async () => {
app = require('../../../server/app')
await app.on('appStarted', () => {
console.log('app started...')
})
requester = chai.request(app).keepOpen()
await app.rateLimiter.resetKey('::ffff:127.0.0.1')
requester = await setupServer()
})

AfterStep(async () => {
Expand Down
24 changes: 9 additions & 15 deletions test/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ const InitMock = require('./testdata/init-mock')
const logger = require('../server/utils/logging')
const config = require('config')
const basicConfig = config.get('passportConfig')
const chai = require('chai')
const chaiHttp = require('chai-http')
chai.use(chaiHttp)

/**
* Mocks external endpoints for app initalization
Expand All @@ -29,22 +32,13 @@ const configureLogger = () => {
/**
* Setup and start server for cucumber test
*/
const setupServer = function () {
return new Promise((resolve, reject) => {
const InitMock = require('../test/testdata/init-mock')
const server = require('../server/app')

const initMock = new InitMock()
initMock.passportConfigEndpoint()
initMock.umaTokenEndpoint()
initMock.umaConfigurationEndpoint()

// waits for the server to start (app.listen)
server.on('appStarted', () => {
console.log('app started...')
return resolve()
})
const setupServer = async function () {
const app = require('../server/app')
await app.on('appStarted', () => {
console.log('app started...')
})
await app.rateLimiter.resetKey('::ffff:127.0.0.1')
return chai.request(app).keepOpen()
}

module.exports = {
Expand Down

0 comments on commit 3ef9db3

Please sign in to comment.