From f54edba6fc41e317db6e067d957d186578de3885 Mon Sep 17 00:00:00 2001 From: Xymorot Date: Mon, 9 Nov 2020 18:11:43 +0100 Subject: [PATCH] test: refactor some methods out of main.spec.ts and add rules to exclude test folders in eslint and forge --- .eslintrc.json | 5 +++-- CONTRIBUTING.md | 1 + forge.config.js | 3 ++- src/main.spec.ts | 15 +++------------ src/main/test/i-application-context.ts | 6 ++++++ src/main/test/spectron-util.ts | 9 +++++++++ 6 files changed, 24 insertions(+), 15 deletions(-) create mode 100644 src/main/test/i-application-context.ts create mode 100644 src/main/test/spectron-util.ts diff --git a/.eslintrc.json b/.eslintrc.json index aedcf38..e95f00f 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -39,7 +39,8 @@ "error", { "devDependencies": [ - "src/**/*.spec.*", + "**/*.{spec,mock}.*", + "src/**/test/*", "mocks/**/*", "src/renderer/**/*", "templates/**/*", @@ -138,7 +139,7 @@ } }, { - "files": ["**/*.{spec,mock}.*"], + "files": ["**/*.{spec,mock}.*", "src/**/test/*.*"], "rules": { "no-unused-expressions": "off", diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index fb13c7a..0c09e6c 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -139,6 +139,7 @@ The testing framework of choice is [Mocha](https://mochajs.org/). Call `npm run - assertion is (mainly) done by [Chai](https://www.chaijs.com/) - Electron specific testing is done by [Spectron](https://electronjs.org/spectron) + - it writes its electron files into a temporary directory, so the local app installation should not be compromised - spies, stubs and mocks are provided by [Sinon.JS](https://sinonjs.org/) - HTTP server mocking is done by [nock](https://github.com/nock/nock) - property based testing is made possible by [fast-check](https://github.com/dubzzz/fast-check) diff --git a/forge.config.js b/forge.config.js index 500c2b2..deb3f9c 100644 --- a/forge.config.js +++ b/forge.config.js @@ -30,7 +30,8 @@ const ignoreList = [ /^\/node_modules\/\.cache($|\/)/, // test and mock files: - /^\/src\/.*\.(spec|mock)\.(ts|js(\.map)?)/, + /^.*\.(spec|mock)\.(ts|js(\.map)?)/, + /^\/src\/.*\/test\/.*$/, // original typescript source and generated source map files: /^\/src\/.*\.(ts|js\.map)/, /^\/src\/.*\.eslintrc\.json/, diff --git a/src/main.spec.ts b/src/main.spec.ts index 271806e..30b3ed4 100644 --- a/src/main.spec.ts +++ b/src/main.spec.ts @@ -1,26 +1,17 @@ -import * as electron from 'electron'; import { expect } from 'chai'; -import { Context } from 'mocha'; import rewiremock from 'rewiremock'; import 'mocha'; -import { Application } from 'spectron'; +import { IApplicationContext } from './main/test/i-application-context'; +import { createApplication } from './main/test/spectron-util'; rewiremock.disable(); describe('Application @spectron', function () { this.timeout(20000); - interface IApplicationContext extends Context { - app?: Application; - } - - // spectron writes its electron files into a temporary directory, the local app installation should not be compromised before(function (this: IApplicationContext) { - this.app = new Application({ - path: ((electron as unknown) as { default: string }).default, - args: ['.', '--enable-logging'], - }); + this.app = createApplication(); return this.app.start(); }); diff --git a/src/main/test/i-application-context.ts b/src/main/test/i-application-context.ts new file mode 100644 index 0000000..45fb405 --- /dev/null +++ b/src/main/test/i-application-context.ts @@ -0,0 +1,6 @@ +import { Context } from 'mocha'; +import { Application } from 'spectron'; + +export interface IApplicationContext extends Context { + app?: Application; +} diff --git a/src/main/test/spectron-util.ts b/src/main/test/spectron-util.ts new file mode 100644 index 0000000..c368c80 --- /dev/null +++ b/src/main/test/spectron-util.ts @@ -0,0 +1,9 @@ +import electron from 'electron'; +import { Application } from 'spectron'; + +export function createApplication(): Application { + return new Application({ + path: ((electron as unknown) as { default: string }).default, + args: ['.', '--enable-logging'], + }); +}