node vs web testing with jasmine

This commit is contained in:
Kyle Spearrin 2018-04-18 12:59:48 -04:00
parent 172f2de4ff
commit 9b4069cb19
9 changed files with 2443 additions and 15 deletions

2346
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -18,8 +18,10 @@
"build:watch": "tsc -watch",
"lint": "tslint src/**/*.ts || true",
"lint:fix": "tslint src/**/*.ts --fix",
"test": "karma start --single-run",
"test:watch": "karma start"
"test": "karma start ./spec/support/karma.conf.js --single-run",
"test:watch": "karma start ./spec/support/karma.conf.js",
"test:node": "npm run build && jasmine",
"test:node:watch": "npm run build:watch & nodemon -w ./dist jasmine"
},
"devDependencies": {
"@types/jasmine": "^2.8.2",
@ -28,6 +30,8 @@
"@types/node-forge": "0.7.1",
"@types/papaparse": "4.1.31",
"@types/webcrypto": "0.0.28",
"jasmine": "^3.1.0",
"jasmine-ts-console-reporter": "^3.1.1",
"jasmine-core": "^2.8.0",
"jasmine-spec-reporter": "^4.2.1",
"karma": "^1.7.1",
@ -37,6 +41,7 @@
"karma-jasmine": "^1.1.0",
"karma-jasmine-html-reporter": "^0.2.2",
"karma-typescript": "^3.0.8",
"nodemon": "^1.17.3",
"rimraf": "^2.6.2",
"tslint": "^5.8.0",
"typescript": "^2.7.1"

3
spec/helpers.ts Normal file
View File

@ -0,0 +1,3 @@
const TSConsoleReporter = require('jasmine-ts-console-reporter');
jasmine.getEnv().clearReporters(); // Clear default console reporter
jasmine.getEnv().addReporter(new TSConsoleReporter());

View File

@ -0,0 +1,62 @@
import { NodeCryptoFunctionService } from '../../../src/services/nodeCryptoFunction.service';
import { UtilsService } from '../../../src/services/utils.service';
describe('NodeCrypto Function Service', () => {
describe('aesEncrypt', () => {
it('should successfully aes encrypt data', async () => {
const nodeCryptoFunctionService = new NodeCryptoFunctionService();
const iv = makeStaticByteArray(16);
const key = makeStaticByteArray(32);
const data = UtilsService.fromUtf8ToArray('EncryptMe!');
const encValue = await nodeCryptoFunctionService.aesEncrypt(data.buffer, iv.buffer, key.buffer);
expect(UtilsService.fromBufferToB64(encValue)).toBe('ByUF8vhyX4ddU9gcooznwA==');
});
});
describe('aesDecryptSmall', () => {
it('should successfully aes decrypt data', async () => {
const nodeCryptoFunctionService = new NodeCryptoFunctionService();
const iv = makeStaticByteArray(16);
const key = makeStaticByteArray(32);
const data = UtilsService.fromB64ToArray('ByUF8vhyX4ddU9gcooznwA==');
const decValue = await nodeCryptoFunctionService.aesDecryptLarge(data.buffer, iv.buffer, key.buffer);
expect(UtilsService.fromBufferToUtf8(decValue)).toBe('EncryptMe!');
});
});
describe('aesDecryptLarge', () => {
it('should successfully aes decrypt data', async () => {
const nodeCryptoFunctionService = new NodeCryptoFunctionService();
const iv = makeStaticByteArray(16);
const key = makeStaticByteArray(32);
const data = UtilsService.fromB64ToArray('ByUF8vhyX4ddU9gcooznwA==');
const decValue = await nodeCryptoFunctionService.aesDecryptLarge(data.buffer, iv.buffer, key.buffer);
expect(UtilsService.fromBufferToUtf8(decValue)).toBe('EncryptMe!');
});
});
describe('randomBytes', () => {
it('should make a value of the correct length', async () => {
const nodeCryptoFunctionService = new NodeCryptoFunctionService();
const randomData = await nodeCryptoFunctionService.randomBytes(16);
expect(randomData.byteLength).toBe(16);
});
it('should not make the same value twice', async () => {
const nodeCryptoFunctionService = new NodeCryptoFunctionService();
const randomData = await nodeCryptoFunctionService.randomBytes(16);
const randomData2 = await nodeCryptoFunctionService.randomBytes(16);
expect(randomData.byteLength === randomData2.byteLength && randomData !== randomData2).toBeTruthy();
});
});
});
function makeStaticByteArray(length: number) {
const arr = new Uint8Array(length);
for (let i = 0; i < length; i++) {
arr[i] = i;
}
return arr;
}

12
spec/support/jasmine.json Normal file
View File

@ -0,0 +1,12 @@
{
"spec_dir": "dist/spec",
"spec_files": [
"common/**/*[sS]pec.js",
"node/**/*[sS]pec.js"
],
"helpers": [
"helpers.js"
],
"stopSpecOnExpectationFailure": false,
"random": true
}

View File

@ -1,7 +1,7 @@
module.exports = function(config) {
config.set({
// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: '',
basePath: '../../',
// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
@ -9,6 +9,8 @@ module.exports = function(config) {
// list of files / patterns to load in the browser
files: [
'spec/common/**/*.ts',
'spec/web/**/*.ts',
'src/abstractions/**/*.ts',
'src/enums/**/*.ts',
'src/models/**/*.ts',
@ -54,9 +56,6 @@ module.exports = function(config) {
karmaTypescriptConfig: {
tsconfig: './tsconfig.json',
compilerOptions: {
module: 'CommonJS'
},
bundlerOptions: {
entrypoints: /\.spec\.ts$/,
sourceMap: true

View File

@ -1,4 +1,4 @@
import { UtilsService } from './utils.service';
import { UtilsService } from '../../../src/services/utils.service';
describe('Utils Service', () => {
describe('getHostname', () => {

View File

@ -1,10 +1,10 @@
import { DeviceType } from '../enums/deviceType';
import { DeviceType } from '../../../src/enums/deviceType';
import { PlatformUtilsService } from '../abstractions/platformUtils.service';
import { PlatformUtilsService } from '../../../src/abstractions/platformUtils.service';
import { WebCryptoFunctionService } from './webCryptoFunction.service';
import { WebCryptoFunctionService } from '../../../src/services/webCryptoFunction.service';
import { UtilsService } from './utils.service';
import { UtilsService } from '../../../src/services/utils.service';
describe('WebCrypto Function Service', () => {
describe('pbkdf2', () => {

View File

@ -1,20 +1,21 @@
{
"compilerOptions": {
"moduleResolution": "node",
"target": "ES2016",
"module": "es6",
"target": "ES6",
"module": "commonjs",
"sourceMap": true,
"declaration": true,
"allowSyntheticDefaultImports": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"declarationDir": "dist/types",
"outDir": "dist/es",
"outDir": "dist",
"typeRoots": [
"node_modules/@types"
]
},
"include": [
"src"
"src",
"spec"
]
}