Best JavaScript code snippet using stryker-parent
plugin-loader.spec.ts
Source:plugin-loader.spec.ts
1import fs from 'fs';2import { syncBuiltinESMExports } from 'module';3import path from 'path';4import { pathToFileURL } from 'url';5import { Plugin, PluginKind } from '@stryker-mutator/api/plugin';6import { expect } from 'chai';7import sinon from 'sinon';8import { factory, testInjector } from '@stryker-mutator/test-helpers';9import { PluginLoader } from '../../../src/di/index.js';10import { fileUtils } from '../../../src/utils/file-utils.js';11import { resolveFromRoot } from '../../helpers/test-utils.js';12import { defaultOptions } from '../../../src/config/index.js';13describe(PluginLoader.name, () => {14 let sut: PluginLoader;15 let importModuleStub: sinon.SinonStub;16 let resolve: sinon.SinonStubbedMember<typeof resolveFromRoot>;17 let readdirStub: sinon.SinonStub;18 beforeEach(() => {19 importModuleStub = sinon.stub(fileUtils, 'importModule');20 readdirStub = sinon.stub(fs.promises, 'readdir');21 resolve = sinon.stub();22 resolve.callsFake((id) => path.resolve(id));23 syncBuiltinESMExports();24 sut = testInjector.injector.injectClass(PluginLoader);25 });26 it('should import modules with a bare specifier', async () => {27 await sut.load(['a', 'b']);28 expect(fileUtils.importModule).calledWith('a');29 expect(fileUtils.importModule).calledWith('b');30 });31 it('should load local plugins using their file URLs', async () => {32 const expectedModuleA = pathToFileURL(path.resolve('a')).toString();33 const expectedModuleReporters = pathToFileURL(path.resolve('../reporter.js')).toString();34 await sut.load(['./a', '../reporter.js']);35 expect(fileUtils.importModule).calledWith(expectedModuleA);36 expect(fileUtils.importModule).calledWith(expectedModuleReporters);37 });38 it('should load absolute path plugins using their file URLs', async () => {39 const absolutePath = path.resolve('./local-test-runner.js');40 const expectedPlugin = pathToFileURL(absolutePath).toString();41 await sut.load([absolutePath]);42 expect(fileUtils.importModule).calledWith(expectedPlugin);43 });44 it('should resolve plugins matching a wildcard inside an organization from the `node_modules` directory', async () => {45 readdirStub.resolves([]);46 await sut.load(['@stryker-mutator/*']);47 expect(readdirStub).calledWith(resolveFromRoot('..', '..', '@stryker-mutator'));48 });49 it('should resolve plugins matching a wildcard from the `node_modules` directory', async () => {50 readdirStub.resolves(['my-prefix-karma-runner', 'some-other-package']);51 await sut.load(['my-prefix-*']);52 expect(readdirStub).calledWith(resolveFromRoot('..', '..'));53 expect(fileUtils.importModule).calledOnce;54 expect(fileUtils.importModule).calledWithExactly('my-prefix-karma-runner');55 });56 it('should load plugins matching a wildcard, ignoring "util", "api", "core" and "instrumenter"', async () => {57 const fooPlugin: SchemaValidationContribution = { strykerValidationSchema: { foo: 'plugin' } };58 importModuleStub.resolves(fooPlugin);59 readdirStub.resolves(['util', 'api', 'core', 'instrumenter', 'typescript-checker', 'karma-runner']);60 await sut.load(['@stryker-mutator/*']);61 expect(fileUtils.importModule).calledTwice;62 expect(fileUtils.importModule).calledWithExactly('@stryker-mutator/typescript-checker');63 expect(fileUtils.importModule).calledWithExactly('@stryker-mutator/karma-runner');64 expect(testInjector.logger.warn).not.called;65 });66 it('should log a warning when a glob expression does not yield modules', async () => {67 readdirStub.resolves(['karma-runner', 'some-other-package']);68 await sut.load(['my-prefix-*']);69 expect(testInjector.logger.warn).calledWithExactly('Expression "%s" not resulted in plugins to load.', 'my-prefix-*');70 });71 it('should not log a warning when the default glob expression does not yield modules', async () => {72 readdirStub.resolves([]);73 await sut.load(defaultOptions.plugins);74 expect(testInjector.logger.warn).not.called;75 });76 it('should log debug information when for glob expression', async () => {77 const fooPlugin: SchemaValidationContribution = { strykerValidationSchema: { foo: 'plugin' } };78 importModuleStub.resolves(fooPlugin);79 readdirStub.resolves(['karma-runner', 'some-other-package']);80 await sut.load(['*']);81 expect(testInjector.logger.debug).calledWithExactly('Loading %s from %s', '*', resolveFromRoot('..', '..'));82 expect(testInjector.logger.debug).calledWithExactly('Loading plugin "%s" (matched with expression %s)', 'karma-runner', '*');83 expect(testInjector.logger.debug).calledWithExactly('Loading plugin "%s" (matched with expression %s)', 'some-other-package', '*');84 expect(testInjector.logger.debug).calledWithExactly('Loading plugin %s', 'karma-runner');85 });86 it('should return the plugin module paths of modules that contributed plugins', async () => {87 const fooPlugin: SchemaValidationContribution = { strykerValidationSchema: { foo: 'plugin' } };88 const barPlugin: PluginModule & SchemaValidationContribution = { strykerValidationSchema: { bar: 'plugin' }, strykerPlugins: [] };89 const bazPlugin = {};90 importModuleStub.withArgs('foo').resolves(fooPlugin).withArgs('bar').resolves(barPlugin).withArgs('baz').resolves(bazPlugin);91 const result = await sut.load(['foo', 'bar', 'baz']);92 expect(result.pluginModulePaths).deep.eq(['bar']);93 });94 it('should return plugins grouped by kind', async () => {95 // Arrange96 const expectedReporters = [97 { kind: PluginKind.Reporter, factory: factory.reporter, name: 'fooReporter' },98 { kind: PluginKind.Reporter, factory: factory.reporter, name: 'bazReporter' },99 ];100 const expectedCheckers = [101 { kind: PluginKind.Checker, factory: factory.checker, name: 'fooChecker' },102 { kind: PluginKind.Checker, factory: factory.checker, name: 'barChecker' },103 ];104 const fooPlugin: PluginModule = { strykerPlugins: [expectedReporters[0], expectedCheckers[0]] };105 const bazPlugin: PluginModule = { strykerPlugins: [expectedReporters[1], expectedCheckers[1]] };106 importModuleStub.withArgs('foo').resolves(fooPlugin).withArgs('baz').resolves(bazPlugin);107 // Act108 const result = await sut.load(['foo', 'baz']);109 // Assert110 expect([...result.pluginsByKind.keys()]).deep.eq([PluginKind.Reporter, PluginKind.Checker]);111 expect(result.pluginsByKind.get(PluginKind.Reporter)).deep.eq(expectedReporters);112 expect(result.pluginsByKind.get(PluginKind.Checker)).deep.eq(expectedCheckers);113 });114 it('should return the schema contributions', async () => {115 const fooPlugin: SchemaValidationContribution = { strykerValidationSchema: { foo: 'plugin' } };116 const barPlugin: PluginModule & SchemaValidationContribution = { strykerValidationSchema: { bar: 'plugin' }, strykerPlugins: [] };117 const bazPlugin = {};118 importModuleStub.withArgs('foo').resolves(fooPlugin).withArgs('bar').resolves(barPlugin).withArgs('baz').resolves(bazPlugin);119 // Act120 const result = await sut.load(['foo', 'bar', 'baz']);121 // Assert122 expect(result.schemaContributions).deep.eq([{ foo: 'plugin' }, { bar: 'plugin' }]);123 });124 it('should not allow non-object schema contributions', async () => {125 importModuleStub.resolves({ strykerValidationSchema: 'error, should be an object' });126 // Act127 const result = await sut.load(['foo']);128 // Assert129 expect(result.schemaContributions).lengthOf(0);130 });131 it('should collect the schema contributions', async () => {132 const fooPlugin: SchemaValidationContribution = { strykerValidationSchema: { foo: 'plugin' } };133 const barPlugin: PluginModule & SchemaValidationContribution = { strykerValidationSchema: { bar: 'plugin' }, strykerPlugins: [] };134 const bazPlugin = {};135 importModuleStub.withArgs('foo').resolves(fooPlugin).withArgs('bar').resolves(barPlugin).withArgs('baz').resolves(bazPlugin);136 // Act137 const result = await sut.load(['foo', 'bar', 'baz']);138 // Assert139 expect(result.schemaContributions).deep.eq([{ foo: 'plugin' }, { bar: 'plugin' }]);140 });141 it('should log ERR_MODULE_NOT_FOUND errors as "cannot find plugin" warnings', async () => {142 importModuleStub.rejects({143 code: 'ERR_MODULE_NOT_FOUND',144 message: "Cannot find package 'a' imported from stryker-js/packages/core/dist/src/utils/file-utils.js",145 });146 await sut.load(['a']);147 expect(testInjector.logger.warn).calledWithExactly('Cannot find plugin "%s".\n Did you forget to install it ?', 'a');148 });149 it('should log ERR_MODULE_NOT_FOUND errors for files other than the file to load as "error during loading..." warnings', async () => {150 importModuleStub.rejects({151 code: 'ERR_MODULE_NOT_FOUND',152 message: "Cannot find package 'a' imported from d.js",153 });154 await sut.load(['b']);155 expect(testInjector.logger.warn).calledWithExactly('Error during loading "%s" plugin:\n %s', 'b', "Cannot find package 'a' imported from d.js");156 });157 it('should log errors during import as "error during loading..." warnings', async () => {158 importModuleStub.rejects({159 code: 'ERR_NOFILE',160 message: "Cannot find package 'a' imported from stryker-js/packages/core/dist/src/utils/file-utils.js",161 });162 await sut.load(['a']);163 expect(testInjector.logger.warn).calledWithExactly(164 'Error during loading "%s" plugin:\n %s',165 'a',166 "Cannot find package 'a' imported from stryker-js/packages/core/dist/src/utils/file-utils.js"167 );168 });169 it('should log a warning when a plugin module did not yield plugins or schema contributions', async () => {170 importModuleStub.resolves({});171 await sut.load(['a']);172 expect(testInjector.logger.warn).calledWithExactly(173 'Module "%s" did not contribute a StrykerJS plugin. It didn\'t export a "%s" or "%s".',174 'a',175 'strykerPlugins',176 'strykerValidationSchema'177 );178 });179});180interface PluginModule {181 strykerPlugins: Array<Plugin<PluginKind>>;182}183interface SchemaValidationContribution {184 strykerValidationSchema: Record<string, unknown>;...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!