mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2024-11-27 03:58:02 -05:00
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:
parent
3b8c85cc9b
commit
ac030760ba
3 changed files with 17 additions and 32 deletions
|
@ -215,11 +215,12 @@
|
|||
"less-loader": "^5.0.0",
|
||||
"mini-css-extract-plugin": "^0.8.0",
|
||||
"mocha": "^5.2.0",
|
||||
"mock-require": "^3.0.3",
|
||||
"optimize-css-assets-webpack-plugin": "^5.0.3",
|
||||
"script-loader": "^0.7.2",
|
||||
"sinon": "^9.0.2",
|
||||
"string-loader": "^0.0.1",
|
||||
"style-loader": "^1.0.0",
|
||||
"ts-mock-imports": "^1.3.0",
|
||||
"ts-node": "^8.8.2",
|
||||
"typescript": "^3.7.2",
|
||||
"url-loader": "^2.3.0",
|
||||
|
|
|
@ -5,7 +5,8 @@ import assert from 'assert'
|
|||
import crypto from 'crypto'
|
||||
import fs from 'fs'
|
||||
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 () {
|
||||
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
|
||||
it('Disable CDN', function () {
|
||||
const testconfig = defaultConfig
|
||||
testconfig.useCDN = false
|
||||
mock('../lib/config', testconfig)
|
||||
csp = mock.reRequire('../lib/csp')
|
||||
ImportMock.mockOther(configModule, 'config', testconfig);
|
||||
|
||||
assert(!csp.computeDirectives().scriptSrc.includes('https://cdnjs.cloudflare.com'))
|
||||
assert(!csp.computeDirectives().scriptSrc.includes('https://cdn.mathjax.org'))
|
||||
|
@ -59,8 +49,7 @@ describe('Content security policies', function () {
|
|||
it('Disable Google Analytics', function () {
|
||||
const testconfig = defaultConfig
|
||||
testconfig.csp.addGoogleAnalytics = false
|
||||
mock('../lib/config', testconfig)
|
||||
csp = mock.reRequire('../lib/csp')
|
||||
ImportMock.mockOther(configModule, 'config', testconfig);
|
||||
|
||||
assert(!csp.computeDirectives().scriptSrc.includes('https://www.google-analytics.com'))
|
||||
})
|
||||
|
@ -68,8 +57,7 @@ describe('Content security policies', function () {
|
|||
it('Disable Disqus', function () {
|
||||
const testconfig = defaultConfig
|
||||
testconfig.csp.addDisqus = false
|
||||
mock('../lib/config', testconfig)
|
||||
csp = mock.reRequire('../lib/csp')
|
||||
ImportMock.mockOther(configModule, 'config', testconfig);
|
||||
|
||||
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 () {
|
||||
const testconfig = defaultConfig
|
||||
testconfig.csp.reportURI = 'https://example.com/reportURI'
|
||||
mock('../lib/config', testconfig)
|
||||
csp = mock.reRequire('../lib/csp')
|
||||
ImportMock.mockOther(configModule, 'config', testconfig);
|
||||
|
||||
assert.strictEqual(csp.computeDirectives().reportUri, 'https://example.com/reportURI')
|
||||
})
|
||||
|
||||
it('Set own directives', function () {
|
||||
const testconfig = defaultConfig
|
||||
mock('../lib/config', defaultConfig)
|
||||
csp = mock.reRequire('../lib/csp')
|
||||
ImportMock.mockOther(configModule, 'config', testconfig);
|
||||
const unextendedCSP = csp.computeDirectives()
|
||||
testconfig.csp.directives = {
|
||||
defaultSrc: ['https://default.example.com'],
|
||||
|
@ -103,8 +89,7 @@ describe('Content security policies', function () {
|
|||
childSrc: ['https://child.example.com'],
|
||||
connectSrc: ['https://connect.example.com']
|
||||
}
|
||||
mock('../lib/config', testconfig)
|
||||
csp = mock.reRequire('../lib/csp')
|
||||
ImportMock.mockOther(configModule, 'config', testconfig);
|
||||
|
||||
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 () {
|
||||
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')
|
||||
})
|
||||
})
|
||||
|
|
|
@ -2,11 +2,13 @@
|
|||
|
||||
'use strict'
|
||||
|
||||
import { ImportMock } from 'ts-mock-imports'
|
||||
import * as configModule from '../lib/config'
|
||||
|
||||
const assert = require('assert')
|
||||
const mock = require('mock-require')
|
||||
const avatars = require('../lib/letter-avatars')
|
||||
|
||||
describe('generateAvatarURL() gravatar enabled', function () {
|
||||
let avatars
|
||||
beforeEach(function () {
|
||||
// Reset config to make sure we don't influence other tests
|
||||
let testconfig = {
|
||||
|
@ -14,8 +16,7 @@ describe('generateAvatarURL() gravatar enabled', function () {
|
|||
serverURL: 'http://localhost:3000',
|
||||
port: 3000
|
||||
}
|
||||
mock('../lib/config', testconfig)
|
||||
avatars = mock.reRequire('../lib/letter-avatars')
|
||||
ImportMock.mockOther(configModule, 'config', testconfig);
|
||||
})
|
||||
|
||||
it('should return correct urls', function () {
|
||||
|
@ -29,7 +30,6 @@ describe('generateAvatarURL() gravatar enabled', function () {
|
|||
})
|
||||
|
||||
describe('generateAvatarURL() gravatar disabled', function () {
|
||||
let avatars
|
||||
beforeEach(function () {
|
||||
// Reset config to make sure we don't influence other tests
|
||||
let testconfig = {
|
||||
|
@ -37,8 +37,7 @@ describe('generateAvatarURL() gravatar disabled', function () {
|
|||
serverURL: 'http://localhost:3000',
|
||||
port: 3000
|
||||
}
|
||||
mock('../lib/config', testconfig)
|
||||
avatars = mock.reRequire('../lib/letter-avatars')
|
||||
ImportMock.mockOther(configModule, 'config', testconfig);
|
||||
})
|
||||
|
||||
it('should return correct urls', function () {
|
||||
|
|
Loading…
Reference in a new issue