Skip to content

Commit

Permalink
wip(esm): updated dynamic imports in tests and updated testdouble stubs
Browse files Browse the repository at this point in the history
for #2543
  • Loading branch information
travi committed Aug 27, 2022
1 parent 21a7b60 commit 38ac59d
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 121 deletions.
4 changes: 4 additions & 0 deletions package.json
Expand Up @@ -8,6 +8,10 @@
"files": [
"test/**/*.test.js"
],
"nodeArguments": [
"--loader=testdouble",
"--no-warnings"
],
"timeout": "2m"
},
"bin": {
Expand Down
43 changes: 27 additions & 16 deletions test/branches/branches.test.js
Expand Up @@ -22,14 +22,17 @@ test('Enforce ranges with branching release workflow', async (t) => {
{name: 'beta', prerelease: true, tags: []},
{name: 'alpha', prerelease: true, tags: []},
];
td.replace('../../lib/branches/get-tags', () => branches);
td.replace('../../lib/branches/expand', () => []);
const getBranches = require('../../lib/branches');
const getTags = await td.replaceEsm('../../lib/branches/get-tags.js');
const expand = await td.replaceEsm('../../lib/branches/expand.js');
const remoteBranches = [];
const context = {options: {branches}};
td.when(expand.default()).thenResolve(remoteBranches);
td.when(getTags.default(context, remoteBranches)).thenResolve(branches);

const getBranches = (await import('../../lib/branches/index.js')).default;
const result = (await getBranches('repositoryUrl', 'master', context))
.map(({name, range}) => ({name, range,}));

let result = (await getBranches('repositoryUrl', 'master', {options: {branches}})).map(({name, range}) => ({
name,
range,
}));
t.is(getBranch(result, '1.0.x').range, '>=1.0.0 <1.0.0', 'Cannot release on 1.0.x before a releasing on master');
t.is(getBranch(result, '1.x').range, '>=1.1.0 <1.0.0', 'Cannot release on 1.x before a releasing on master');
t.is(getBranch(result, 'master').range, '>=1.0.0');
Expand Down Expand Up @@ -203,7 +206,7 @@ test('Throw SemanticReleaseError for invalid configurations', async (t) => {
];
td.replace('../../lib/branches/get-tags', () => branches);
td.replace('../../lib/branches/expand', () => []);
const getBranches = require('../../lib/branches');
const getBranches = (await import('../../lib/branches/index.js')).default;
const errors = [...(await t.throwsAsync(getBranches('repositoryUrl', 'master', {options: {branches}})))];

t.is(errors[0].name, 'SemanticReleaseError');
Expand Down Expand Up @@ -233,11 +236,15 @@ test('Throw a SemanticReleaseError if there is duplicate branches', async (t) =>
{name: 'master', tags: []},
{name: 'master', tags: []},
];
td.replace('../../lib/branches/get-tags', () => branches);
td.replace('../../lib/branches/expand', () => []);
const getBranches = require('../../lib/branches');
const getTags = await td.replaceEsm('../../lib/branches/get-tags.js');
const expand = await td.replaceEsm('../../lib/branches/expand.js');
const context = {options: {branches}};
const remoteBranches = [];
td.when(expand.default()).thenResolve(remoteBranches);
td.when(getTags.default(context, remoteBranches)).thenResolve(branches);
const getBranches = (await import('../../lib/branches/index.js')).default;

const errors = [...(await t.throwsAsync(getBranches('repositoryUrl', 'master', {options: {branches}})))];
const errors = [...(await t.throwsAsync(getBranches('repositoryUrl', 'master', context)))];

t.is(errors[0].name, 'SemanticReleaseError');
t.is(errors[0].code, 'EDUPLICATEBRANCHES');
Expand All @@ -250,11 +257,15 @@ test('Throw a SemanticReleaseError for each invalid branch name', async (t) => {
{name: '~master', tags: []},
{name: '^master', tags: []},
];
td.replace('../../lib/branches/get-tags', () => branches);
td.replace('../../lib/branches/expand', () => []);
const getBranches = require('../../lib/branches');
const getTags = await td.replaceEsm('../../lib/branches/get-tags.js');
const expand = await td.replaceEsm('../../lib/branches/expand.js');
const context = {options: {branches}};
const remoteBranches = [];
td.when(expand.default()).thenResolve(remoteBranches);
td.when(getTags.default(context, remoteBranches)).thenResolve(branches);
const getBranches = (await import('../../lib/branches/index.js')).default;

const errors = [...(await t.throwsAsync(getBranches('repositoryUrl', 'master', {options: {branches}})))];
const errors = [...(await t.throwsAsync(getBranches('repositoryUrl', 'master', context)))];

t.is(errors[0].name, 'SemanticReleaseError');
t.is(errors[0].code, 'EINVALIDBRANCHNAME');
Expand Down
6 changes: 3 additions & 3 deletions test/get-config.test.js
Expand Up @@ -17,10 +17,10 @@ const DEFAULT_PLUGINS = [
'@semantic-release/github',
];

test.beforeEach((t) => {
test.beforeEach(async (t) => {
t.context.plugins = stub().returns({});
td.replace('../lib/plugins', t.context.plugins);
t.context.getConfig = require('../lib/get-config');
await td.replaceEsm('../lib/plugins.js', null, t.context.plugins);
t.context.getConfig = (await import('../lib/get-config.js')).default;
});

test('Default values, reading repositoryUrl from package.json', async (t) => {
Expand Down

0 comments on commit 38ac59d

Please sign in to comment.