Skip to content

Commit

Permalink
chore: fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MarshallOfSound committed Oct 31, 2022
1 parent cccdfc6 commit af13a32
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 21 deletions.
2 changes: 1 addition & 1 deletion packages/api/core/test/fast/make_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('make', () => {
before(() => {
const electronPath = path.resolve(__dirname, 'node_modules/electron');
stubbedMake = proxyquire.noCallThru().load('../../src/api/make', {
'../util/electron-version': {
'@electron-forge/core-utils': {
getElectronModulePath: () => Promise.resolve(electronPath),
getElectronVersion: () => Promise.resolve('1.0.0'),
},
Expand Down
2 changes: 1 addition & 1 deletion packages/api/core/test/fast/start_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('start', () => {

start = proxyquire.noCallThru().load('../../src/api/start', {
'../util/electron-executable': () => Promise.resolve('fake_electron_path'),
'../util/electron-version': {
'@electron-forge/core-utils': {
getElectronVersion: () => Promise.resolve('1.0.0'),
},
'../util/forge-config': async () => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ class TypeScriptWebpackTemplate extends BaseTemplate {
const filePath = (fileName: string) => path.join(directory, 'src', fileName);

// Copy Webpack files
await this.copyTemplateFile(directory, 'webpack.main.config.js');
await this.copyTemplateFile(directory, 'webpack.renderer.config.js');
await this.copyTemplateFile(directory, 'webpack.rules.js');
await this.copyTemplateFile(directory, 'webpack.plugins.js');
await this.copyTemplateFile(directory, 'webpack.main.config.ts');
await this.copyTemplateFile(directory, 'webpack.renderer.config.ts');
await this.copyTemplateFile(directory, 'webpack.rules.ts');
await this.copyTemplateFile(directory, 'webpack.plugins.ts');

await this.updateFileByLine(path.resolve(directory, 'src', 'index.html'), (line) => {
if (line.includes('link rel="stylesheet"')) return '';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { expect } from 'chai';
import glob from 'fast-glob';
import fs from 'fs-extra';

import { api } from '../../../api/core';
import template from '../src/TypeScriptWebpackTemplate';

describe('TypeScriptWebpackTemplate', () => {
Expand All @@ -15,17 +16,22 @@ describe('TypeScriptWebpackTemplate', () => {
});

it('should succeed in initializing the typescript template', async () => {
await template.initializeTemplate(dir, {});
await api.init({
dir,
template: path.resolve(__dirname, '..', 'src', 'TypeScriptWebpackTemplate'),
interactive: false,
});
});

context('template files are copied to project', () => {
const expectedFiles = [
'tsconfig.json',
'.eslintrc.json',
'webpack.main.config.js',
'webpack.renderer.config.js',
'webpack.rules.js',
'webpack.plugins.js',
'forge.config.ts',
'webpack.main.config.ts',
'webpack.renderer.config.ts',
'webpack.rules.ts',
'webpack.plugins.ts',
path.join('src', 'index.ts'),
path.join('src', 'renderer.ts'),
path.join('src', 'preload.ts'),
Expand All @@ -44,10 +50,13 @@ describe('TypeScriptWebpackTemplate', () => {

describe('lint', () => {
before(async () => {
delete process.env.TS_NODE_PROJECT;
await testUtils.ensureModulesInstalled(
dir,
['electron', 'electron-squirrel-startup'],
template.devDependencies.filter((moduleName) => moduleName.includes('eslint') || moduleName.includes('typescript'))
template.devDependencies
.filter((moduleName) => moduleName.includes('eslint') || moduleName.includes('typescript'))
.concat(['@electron-forge/plugin-webpack'])
);
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type IForkTsCheckerWebpackPlugin from 'fork-ts-checker-webpack-plugin';

// eslint-disable-next-line @typescript-eslint/no-var-requires
const ForkTsCheckerWebpackPlugin: typeof IForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');

export const plugins = [new ForkTsCheckerWebpackPlugin()];
22 changes: 13 additions & 9 deletions packages/utils/core-utils/test/electron-version_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ import fs from 'fs-extra';
import { devDeps, exactDevDeps } from '../../../api/core/src/api/init-scripts/init-npm';
import { getElectronModulePath, getElectronVersion, updateElectronDependency } from '../src/electron-version';

const fixturePath = path.resolve(__dirname, '..', '..', '..', 'api', 'core', 'test', 'fixture');

describe('updateElectronDependency', () => {
it('adds an Electron dep if one does not already exist', () => {
const packageJSON = { dependencies: {}, devDependencies: {} };
const [dev, exact] = updateElectronDependency(packageJSON, devDeps, exactDevDeps);
expect(dev).to.deep.equal(devDeps);
expect(exact).to.deep.equal(exactDevDeps);
});

it('does not add an Electron dep if one already exists', () => {
const packageJSON = {
dependencies: {},
Expand All @@ -23,6 +26,7 @@ describe('updateElectronDependency', () => {
expect(dev).to.deep.equal(devDeps);
expect(exact).to.deep.equal([]);
});

it('moves an Electron dependency from dependencies to devDependencies', () => {
const packageJSON = {
dependencies: { electron: '0.37.0' },
Expand All @@ -41,12 +45,12 @@ describe('getElectronVersion', () => {
expect(getElectronVersion('', { devDependencies: {} })).to.eventually.be.rejectedWith('Electron packages in devDependencies'));

it('fails with a non-exact version and no electron installed', () => {
const fixtureDir = path.resolve(__dirname, '..', 'fixture', 'dummy_app');
const fixtureDir = path.resolve(fixturePath, 'dummy_app');
return expect(getElectronVersion(fixtureDir, { devDependencies: { electron: '^4.0.2' } })).to.eventually.be.rejectedWith('Cannot find the package');
});

it('works with a non-exact version with electron installed', () => {
const fixtureDir = path.resolve(__dirname, '..', 'fixture', 'non-exact');
const fixtureDir = path.resolve(fixturePath, 'non-exact');
return expect(getElectronVersion(fixtureDir, { devDependencies: { electron: '^4.0.2' } })).to.eventually.equal('4.0.9');
});

Expand Down Expand Up @@ -84,7 +88,7 @@ describe('getElectronVersion', () => {
});

it('works with a non-exact version', async () => {
const fixtureDir = path.resolve(__dirname, '..', 'fixture', 'yarn-workspace', 'packages', 'subpackage');
const fixtureDir = path.resolve(fixturePath, 'yarn-workspace', 'packages', 'subpackage');
const packageJSON = {
devDependencies: { electron: '^4.0.4' },
};
Expand All @@ -111,7 +115,7 @@ describe('getElectronModulePath', () => {
});

it('throws an error saying it cannot find electron', async () => {
const fixtureDir = path.resolve(__dirname, '..', 'fixture', 'dummy_app');
const fixtureDir = path.resolve(fixturePath, 'dummy_app');
await fs.copy(fixtureDir, tempDir);
return expect(getElectronModulePath(tempDir, { devDependencies: { electron: '^4.0.2' } })).to.eventually.be.rejectedWith('Cannot find the package');
});
Expand All @@ -122,7 +126,7 @@ describe('getElectronModulePath', () => {
});

it('works with electron', () => {
const fixtureDir = path.resolve(__dirname, '..', 'fixture', 'non-exact');
const fixtureDir = path.resolve(fixturePath, 'non-exact');
return expect(getElectronModulePath(fixtureDir, { devDependencies: { electron: '^4.0.2' } })).to.eventually.equal(
path.join(fixtureDir, 'node_modules', 'electron')
);
Expand All @@ -134,7 +138,7 @@ describe('getElectronModulePath', () => {
});

it('finds the top-level electron module', async () => {
const workspaceDir = path.resolve(__dirname, '..', 'fixture', 'npm-workspace');
const workspaceDir = path.resolve(fixturePath, 'npm-workspace');
const fixtureDir = path.join(workspaceDir, 'packages', 'subpackage');
const packageJSON = {
devDependencies: { electron: '^4.0.4' },
Expand All @@ -154,7 +158,7 @@ describe('getElectronModulePath', () => {
});

it('finds the top-level electron module', async () => {
const workspaceDir = path.resolve(__dirname, '..', 'fixture', 'yarn-workspace');
const workspaceDir = path.resolve(fixturePath, 'yarn-workspace');
const fixtureDir = path.join(workspaceDir, 'packages', 'subpackage');
const packageJSON = {
devDependencies: { electron: '^4.0.4' },
Expand All @@ -164,7 +168,7 @@ describe('getElectronModulePath', () => {
});

it('finds the top-level electron module despite the additional node_modules folder inside the package', async () => {
const workspaceDir = path.resolve(__dirname, '..', 'fixture', 'yarn-workspace');
const workspaceDir = path.resolve(fixturePath, 'yarn-workspace');
const fixtureDir = path.join(workspaceDir, 'packages', 'with-node-modules');
const packageJSON = {
devDependencies: { electron: '^4.0.4' },
Expand All @@ -174,7 +178,7 @@ describe('getElectronModulePath', () => {
});

it('finds the correct electron module in nohoist mode', async () => {
const workspaceDir = path.resolve(__dirname, '..', 'fixture', 'yarn-workspace');
const workspaceDir = path.resolve(fixturePath, 'yarn-workspace');
const fixtureDir = path.join(workspaceDir, 'packages', 'electron-folder-in-node-modules');
const packageJSON = {
devDependencies: { electron: '^13.0.0' },
Expand Down

0 comments on commit af13a32

Please sign in to comment.