Fix mocha tests in TypeScript

`mock-require` does not work with TypeScript, as the compiled JS expects a sub-object: `import { config } from Config` compiles to `const config_1 = require("./config")`, but the config object is now in `config_1.config`, *not* in `config_1` directly.

Therefore `mock-require` was replaced with `ts-mock-imports`, which also simplifies the code a bit.

Signed-off-by: David Mehren <dmehren1@gmail.com>
This commit is contained in:
David Mehren 2020-05-09 13:50:53 +02:00
parent 3b8c85cc9b
commit ac030760ba
No known key found for this signature in database
GPG key ID: 6017AF117F9756CB
3 changed files with 17 additions and 32 deletions

View file

@ -215,11 +215,12 @@
"less-loader": "^5.0.0", "less-loader": "^5.0.0",
"mini-css-extract-plugin": "^0.8.0", "mini-css-extract-plugin": "^0.8.0",
"mocha": "^5.2.0", "mocha": "^5.2.0",
"mock-require": "^3.0.3",
"optimize-css-assets-webpack-plugin": "^5.0.3", "optimize-css-assets-webpack-plugin": "^5.0.3",
"script-loader": "^0.7.2", "script-loader": "^0.7.2",
"sinon": "^9.0.2",
"string-loader": "^0.0.1", "string-loader": "^0.0.1",
"style-loader": "^1.0.0", "style-loader": "^1.0.0",
"ts-mock-imports": "^1.3.0",
"ts-node": "^8.8.2", "ts-node": "^8.8.2",
"typescript": "^3.7.2", "typescript": "^3.7.2",
"url-loader": "^2.3.0", "url-loader": "^2.3.0",

View file

@ -5,7 +5,8 @@ import assert from 'assert'
import crypto from 'crypto' import crypto from 'crypto'
import fs from 'fs' import fs from 'fs'
import path from 'path' import path from 'path'
import mock from 'mock-require' import * as configModule from '../lib/config';
import { ImportMock } from 'ts-mock-imports';
describe('Content security policies', function () { describe('Content security policies', function () {
let defaultConfig, csp let defaultConfig, csp
@ -31,22 +32,11 @@ describe('Content security policies', function () {
} }
}) })
afterEach(function () {
mock.stop('../lib/config')
csp = mock.reRequire('../lib/csp')
})
after(function () {
mock.stopAll()
csp = mock.reRequire('../lib/csp')
})
// beginnging Tests // beginnging Tests
it('Disable CDN', function () { it('Disable CDN', function () {
const testconfig = defaultConfig const testconfig = defaultConfig
testconfig.useCDN = false testconfig.useCDN = false
mock('../lib/config', testconfig) ImportMock.mockOther(configModule, 'config', testconfig);
csp = mock.reRequire('../lib/csp')
assert(!csp.computeDirectives().scriptSrc.includes('https://cdnjs.cloudflare.com')) assert(!csp.computeDirectives().scriptSrc.includes('https://cdnjs.cloudflare.com'))
assert(!csp.computeDirectives().scriptSrc.includes('https://cdn.mathjax.org')) assert(!csp.computeDirectives().scriptSrc.includes('https://cdn.mathjax.org'))
@ -59,8 +49,7 @@ describe('Content security policies', function () {
it('Disable Google Analytics', function () { it('Disable Google Analytics', function () {
const testconfig = defaultConfig const testconfig = defaultConfig
testconfig.csp.addGoogleAnalytics = false testconfig.csp.addGoogleAnalytics = false
mock('../lib/config', testconfig) ImportMock.mockOther(configModule, 'config', testconfig);
csp = mock.reRequire('../lib/csp')
assert(!csp.computeDirectives().scriptSrc.includes('https://www.google-analytics.com')) assert(!csp.computeDirectives().scriptSrc.includes('https://www.google-analytics.com'))
}) })
@ -68,8 +57,7 @@ describe('Content security policies', function () {
it('Disable Disqus', function () { it('Disable Disqus', function () {
const testconfig = defaultConfig const testconfig = defaultConfig
testconfig.csp.addDisqus = false testconfig.csp.addDisqus = false
mock('../lib/config', testconfig) ImportMock.mockOther(configModule, 'config', testconfig);
csp = mock.reRequire('../lib/csp')
assert(!csp.computeDirectives().scriptSrc.includes('https://disqus.com')) assert(!csp.computeDirectives().scriptSrc.includes('https://disqus.com'))
assert(!csp.computeDirectives().scriptSrc.includes('https://*.disqus.com')) assert(!csp.computeDirectives().scriptSrc.includes('https://*.disqus.com'))
@ -81,16 +69,14 @@ describe('Content security policies', function () {
it('Set ReportURI', function () { it('Set ReportURI', function () {
const testconfig = defaultConfig const testconfig = defaultConfig
testconfig.csp.reportURI = 'https://example.com/reportURI' testconfig.csp.reportURI = 'https://example.com/reportURI'
mock('../lib/config', testconfig) ImportMock.mockOther(configModule, 'config', testconfig);
csp = mock.reRequire('../lib/csp')
assert.strictEqual(csp.computeDirectives().reportUri, 'https://example.com/reportURI') assert.strictEqual(csp.computeDirectives().reportUri, 'https://example.com/reportURI')
}) })
it('Set own directives', function () { it('Set own directives', function () {
const testconfig = defaultConfig const testconfig = defaultConfig
mock('../lib/config', defaultConfig) ImportMock.mockOther(configModule, 'config', testconfig);
csp = mock.reRequire('../lib/csp')
const unextendedCSP = csp.computeDirectives() const unextendedCSP = csp.computeDirectives()
testconfig.csp.directives = { testconfig.csp.directives = {
defaultSrc: ['https://default.example.com'], defaultSrc: ['https://default.example.com'],
@ -103,8 +89,7 @@ describe('Content security policies', function () {
childSrc: ['https://child.example.com'], childSrc: ['https://child.example.com'],
connectSrc: ['https://connect.example.com'] connectSrc: ['https://connect.example.com']
} }
mock('../lib/config', testconfig) ImportMock.mockOther(configModule, 'config', testconfig);
csp = mock.reRequire('../lib/csp')
const variations = ['default', 'script', 'img', 'style', 'font', 'object', 'media', 'child', 'connect'] const variations = ['default', 'script', 'img', 'style', 'font', 'object', 'media', 'child', 'connect']
@ -118,7 +103,7 @@ describe('Content security policies', function () {
*/ */
it('Unchanged hash for reveal.js speaker notes plugin', function () { it('Unchanged hash for reveal.js speaker notes plugin', function () {
const hash = crypto.createHash('sha1') const hash = crypto.createHash('sha1')
hash.update(fs.readFileSync(path.resolve(__dirname, '../node_modules/reveal.js/plugin/notes/notes.html'), 'utf8'), 'utf8') hash.update(fs.readFileSync(path.join(process.cwd(), '/node_modules/reveal.js/plugin/notes/notes.html'), 'utf8'), 'utf8')
assert.strictEqual(hash.digest('hex'), 'd5d872ae49b5db27f638b152e6e528837204d380') assert.strictEqual(hash.digest('hex'), 'd5d872ae49b5db27f638b152e6e528837204d380')
}) })
}) })

View file

@ -2,11 +2,13 @@
'use strict' 'use strict'
import { ImportMock } from 'ts-mock-imports'
import * as configModule from '../lib/config'
const assert = require('assert') const assert = require('assert')
const mock = require('mock-require') const avatars = require('../lib/letter-avatars')
describe('generateAvatarURL() gravatar enabled', function () { describe('generateAvatarURL() gravatar enabled', function () {
let avatars
beforeEach(function () { beforeEach(function () {
// Reset config to make sure we don't influence other tests // Reset config to make sure we don't influence other tests
let testconfig = { let testconfig = {
@ -14,8 +16,7 @@ describe('generateAvatarURL() gravatar enabled', function () {
serverURL: 'http://localhost:3000', serverURL: 'http://localhost:3000',
port: 3000 port: 3000
} }
mock('../lib/config', testconfig) ImportMock.mockOther(configModule, 'config', testconfig);
avatars = mock.reRequire('../lib/letter-avatars')
}) })
it('should return correct urls', function () { it('should return correct urls', function () {
@ -29,7 +30,6 @@ describe('generateAvatarURL() gravatar enabled', function () {
}) })
describe('generateAvatarURL() gravatar disabled', function () { describe('generateAvatarURL() gravatar disabled', function () {
let avatars
beforeEach(function () { beforeEach(function () {
// Reset config to make sure we don't influence other tests // Reset config to make sure we don't influence other tests
let testconfig = { let testconfig = {
@ -37,8 +37,7 @@ describe('generateAvatarURL() gravatar disabled', function () {
serverURL: 'http://localhost:3000', serverURL: 'http://localhost:3000',
port: 3000 port: 3000
} }
mock('../lib/config', testconfig) ImportMock.mockOther(configModule, 'config', testconfig);
avatars = mock.reRequire('../lib/letter-avatars')
}) })
it('should return correct urls', function () { it('should return correct urls', function () {