semantic-release/test/get-release-type.test.js
Pierre-Denis Vanduynslager abf92ad03d refactor: Use ES6, Test with AVA
- Use async/await instead of callbacks
- Use execa to run command line
- Use AVA for tests
- Add several assertions in the unit tests
- Add documentation (comments) in the tests
- Run tests with a real git repo instead of mocking child_process and add test helpers to create repos, commits and checkout
- Simplify test directory structure
- Simplify code readability (mostly with async/await)
- Use eslint for for linting, prettier for formatting
2017-09-30 13:30:41 -04:00

88 lines
3.0 KiB
JavaScript

import {callbackify} from 'util';
import test from 'ava';
import {stub} from 'sinon';
import SemanticReleaseError from '@semantic-release/error';
import getReleaseType from '../src/lib/get-release-type';
test('Get commit types from commits', async t => {
// Stub the commitAnalyzer plugin, returns 'major' release type
const analyzeCommits = stub().resolves('major');
const commits = [{hash: '0', message: 'a'}];
// Call the get-release-type module
const releaseType = await getReleaseType({
commits,
lastRelease: {version: '1.0.0'},
plugins: {analyzeCommits: callbackify(analyzeCommits)},
});
// Verify the module return the release type obtain from the commitAnalyzer plugin
t.is(releaseType, 'major');
// Verify the commitAnalyzer plugin was called with the commits
t.true(analyzeCommits.calledOnce);
t.deepEqual(analyzeCommits.firstCall.args[0].commits, commits);
});
test('Throws error when no changes', async t => {
// Stub the commitAnalyzer plugin, returns 'null' release type
const analyzeCommits = stub().resolves(null);
const commits = [{hash: '0', message: 'a'}];
// Call the get-release-type module and verify it returns an error
const error = await t.throws(
getReleaseType({
commits,
lastRelease: {version: '1.0.0'},
plugins: {analyzeCommits: callbackify(analyzeCommits)},
})
);
// Verify the error code adn type
t.is(error.code, 'ENOCHANGE');
t.true(error instanceof SemanticReleaseError);
// Verify the commitAnalyzer plugin was called with the commits
t.true(analyzeCommits.calledOnce);
t.deepEqual(analyzeCommits.firstCall.args[0].commits, commits);
});
test('Return initial if there is no lastRelease', async t => {
// Stub the commitAnalyzer plugin, returns 'major' release type
const analyzeCommits = stub().resolves('major');
const commits = [{hash: '0', message: 'a'}];
// Call the get-release-type module
const releaseType = await getReleaseType({
commits,
lastRelease: {},
plugins: {analyzeCommits: callbackify(analyzeCommits)},
});
// Verify the module return an initial release type
t.is(releaseType, 'initial');
// Verify the commitAnalyzer plugin was called with the commits
t.true(analyzeCommits.calledOnce);
t.deepEqual(analyzeCommits.firstCall.args[0].commits, commits);
});
test('Throws error when no changes even if there is no lastRelease', async t => {
// Stub the commitAnalyzer plugin, returns 'null' release type
const analyzeCommits = stub().resolves(null);
const commits = [{hash: '0', message: 'a'}];
// Call the get-release-type module and verify it returns an error
const error = await t.throws(
getReleaseType({commits, lastRelease: {}, plugins: {analyzeCommits: callbackify(analyzeCommits)}})
);
// Verify the error code adn type
t.is(error.code, 'ENOCHANGE');
t.true(error instanceof SemanticReleaseError);
// Verify the commitAnalyzer plugin was called with the commits
t.true(analyzeCommits.calledOnce);
t.deepEqual(analyzeCommits.firstCall.args[0].commits, commits);
});