Skip to content

Commit

Permalink
Adding another batch of unit tests (#34)
Browse files Browse the repository at this point in the history
* adding tests for the maintenance plugin

* adding tests for the license plugin

* adding tests for the licenseTree plugin

* adding tests for the support plugin
  • Loading branch information
aalykiot authored Jul 6, 2021
1 parent e69cd08 commit c551190
Show file tree
Hide file tree
Showing 4 changed files with 431 additions and 0 deletions.
107 changes: 107 additions & 0 deletions test/plugins/license.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/* eslint-env jest */

const { success, warning, failure } = require('../../src/lib/format');
const licensePlugin = require('../../src/plugins/license');

jest.mock('../../src/lib/format', () => ({
...jest.requireActual('../../src/lib/format'),
failure: jest.fn(),
success: jest.fn(),
warning: jest.fn()
}));

afterEach(() => {
jest.clearAllMocks();
});

it('should return null when license is globally accepted', async () => {
// create a sample env with some dummy data
const testEnv = {
pkg: {
name: 'test',
license: 'MIT'
},
config: {
licenses: {
allow: ['MIT', 'Apache-2.0'],
rules: {}
}
}
};

const result = await licensePlugin(testEnv.pkg, testEnv.config);

expect(result).toBe(null);
expect(success).toHaveBeenCalled();
});

it('should return null when the license is locally accepted', async () => {
// create a sample env with some dummy data
const testEnv = {
pkg: {
name: 'test',
license: 'Apache-2.0'
},
config: {
licenses: {
allow: ['MIT'],
rules: {
test: {
allow: ['Apache-2.0']
}
}
}
}
};

const result = await licensePlugin(testEnv.pkg, testEnv.config);

expect(result).toBe(null);
expect(success).toHaveBeenCalled();
});

it('should return a warning when the license is locally overridden', async () => {
// create a sample env with some dummy data
const testEnv = {
pkg: {
name: 'test',
license: 'Apache-2.0'
},
config: {
licenses: {
allow: ['MIT'],
rules: {
test: {
override: ['Apache-2.0']
}
}
}
}
};

const result = await licensePlugin(testEnv.pkg, testEnv.config);

expect(result.type).toBe('warning');
expect(warning).toHaveBeenCalled();
});

it('should return an error when the license is not accepted', async () => {
// create a sample env with some dummy data
const testEnv = {
pkg: {
name: 'test',
license: 'Apache-2.0'
},
config: {
licenses: {
allow: ['MIT'],
rules: {}
}
}
};

const result = await licensePlugin(testEnv.pkg, testEnv.config);

expect(result.type).toBe('error');
expect(failure).toHaveBeenCalled();
});
172 changes: 172 additions & 0 deletions test/plugins/licenseTree.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
/* eslint-env jest */

const licenseChecker = require('license-checker');
const licenseTreePlugin = require('../../src/plugins/licenseTree');
const { success, warning, failure } = require('../../src/lib/format');

jest.mock('license-checker');

jest.mock('../../src/lib/format', () => ({
...jest.requireActual('../../src/lib/format'),
failure: jest.fn(),
success: jest.fn(),
warning: jest.fn()
}));

afterEach(() => {
jest.clearAllMocks();
});

it('should return an empty list when the dep-tree has acceptable licenses', async () => {
// mock licenseChecker's functionality
licenseChecker.init.mockImplementation((_, cb) => {
const packages = {
dummy: {
licenses: 'MIT',
repository: 'https://github.com/dummy/dummy'
},
dummy2: {
licenses: 'MIT',
repository: 'https://github.com/dummy2/dummy2'
}
};
cb(null, packages);
});

// create a sample env with some dummy data
const testEnv = {
pkg: {
name: 'test'
},
config: {
licenses: {
allow: ['MIT', 'Apache-2.0'],
rules: {}
}
}
};

const result = await licenseTreePlugin(testEnv.pkg, testEnv.config);

expect(result).toEqual([]);
expect(success).toBeCalledTimes(2);
});

it('should return a list of errors when deps have non-acceptable licenses', async () => {
// mock licenseChecker's functionality
licenseChecker.init.mockImplementation((_, cb) => {
const packages = {
dummy: {
licenses: 'BSD',
repository: 'https://github.com/dummy/dummy'
},
dummy2: {
licenses: 'BSD',
repository: 'https://github.com/dummy2/dummy2'
}
};
cb(null, packages);
});

// create a sample env with some dummy data
const testEnv = {
pkg: {
name: 'test'
},
config: {
licenses: {
allow: ['MIT', 'Apache-2.0'],
rules: {}
}
}
};

const result = await licenseTreePlugin(testEnv.pkg, testEnv.config);

expect(result.length).toBe(2);
expect(result[0].type).toBe('error');
expect(result[1].type).toBe('error');
expect(failure).toHaveBeenCalledTimes(2);
});

it('should return an empty list when deps have "local" acceptable licenses', async () => {
// mock licenseChecker's functionality
licenseChecker.init.mockImplementation((_, cb) => {
const packages = {
dummy: {
licenses: 'BSD',
repository: 'https://github.com/dummy/dummy'
},
dummy2: {
licenses: 'BSD',
repository: 'https://github.com/dummy2/dummy2'
}
};
cb(null, packages);
});

// create a sample env with some dummy data
const testEnv = {
pkg: {
name: 'test',
license: 'Apache-2.0'
},
config: {
licenses: {
allow: ['MIT', 'Apache-2.0'],
rules: {
test: {
allow: ['BSD']
}
}
}
}
};

const result = await licenseTreePlugin(testEnv.pkg, testEnv.config);

expect(result).toEqual([]);
expect(success).toBeCalledTimes(2);
});

it('should return a list of warnings when deps have "non-decidable" licenses', async () => {
// mock licenseChecker's functionality
licenseChecker.init.mockImplementation((_, cb) => {
const packages = {
dummy: {
licenses: 'BSD',
repository: 'https://github.com/dummy/dummy'
},
dummy2: {
licenses: 'BSD',
repository: 'https://github.com/dummy2/dummy2'
}
};
cb(null, packages);
});

// create a sample env with some dummy data
const testEnv = {
pkg: {
name: 'test',
license: 'Apache-2.0'
},
config: {
licenses: {
allow: ['MIT', 'Apache-2.0'],
rules: {
test: {
override: ['BSD']
}
}
}
}
};

const result = await licenseTreePlugin(testEnv.pkg, testEnv.config);

expect(result.length).toBe(2);
expect(result[0].type).toBe('warning');
expect(result[1].type).toBe('warning');
expect(warning).toHaveBeenCalledTimes(2);
});
66 changes: 66 additions & 0 deletions test/plugins/maintenance.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/* eslint-env jest */

const network = require('../../src/lib/fetch');
const { success, warning } = require('../../src/lib/format');
const maintenancePlugin = require('../../src/plugins/maintenance');

jest.mock('../../src/lib/fetch');
jest.mock('../../src/lib/format', () => ({
...jest.requireActual('../../src/lib/format'),
failure: jest.fn(),
success: jest.fn(),
warning: jest.fn()
}));

afterEach(() => {
jest.clearAllMocks();
});

it('should return null when module has a recent release', async () => {
network.fetchGithub.mockImplementation(() => {
return [{ published_at: new Date() }];
});

const pkg = {
repository: {
url: 'git+https://github.com/test/test.git'
}
};

const result = await maintenancePlugin(pkg, null, {});

expect(result).toBe(null);
expect(success).toHaveBeenCalled();
});

it('should return a warning when release is more than six months old', async () => {
network.fetchGithub.mockImplementation(() => {
return [{ published_at: new Date(new Date() - 123456789123) }];
});

const pkg = {
repository: {
url: 'git+https://github.com/test/test.git'
}
};

const result = await maintenancePlugin(pkg, null, {});
expect(result.type).toBe('warning');
expect(warning).toHaveBeenCalled();
});

it('should return a warning when a module has no releases', async () => {
network.fetchGithub.mockImplementation(() => {
return [];
});

const pkg = {
repository: {
url: 'git+https://github.com/test/test.git'
}
};

const result = await maintenancePlugin(pkg, null, {});
expect(result.type).toBe('warning');
expect(warning).toHaveBeenCalled();
});
Loading

0 comments on commit c551190

Please sign in to comment.