UNPKG

semantic-release-github-notifier

Version:

semantic-release-github plugin that notifies GitHub issues and pull requests of a related package release.

249 lines (189 loc) 8.14 kB
'use strict'; /* eslint-disable no-unused-expressions */ const chai = require(`chai`); const chaiAsPromised = require(`chai-as-promised`); const mocha = require(`mocha`); const nock = require(`nock`); const sinonChai = require(`sinon-chai`); chai.use(chaiAsPromised); chai.use(sinonChai); const expect = chai.expect; const before = mocha.before; const beforeEach = mocha.beforeEach; const describe = mocha.describe; const it = mocha.it; describe(`semantic-release-github-notifier`, function () { before(function () { nock.disableNetConnect(); }); beforeEach(function () { this.config = { data: { commits: [ `chore(package): initial setup\n\n`, `docs(README): add an introduction paragraph\n Add introduction paragraph for the \`semantic-release-github\` project.\n Closes #1\n\n`, `docs(README): add 'as-is' statement\n Add \`as-is\` statement to README.md.\n Closes #2\n\n`, `Merge pull request #4 from origin/docs/readme/as-is\n docs(README): add 'as-is' statement\n\n`, `chore(gitignore): add\n Add a \`.gitignore\` file.`, `Merge pull request #5 from origin/chore/gitignore/add\n chore(gitignore): add\n Closes #3\n\n`, ], version: `1.0.0`, }, options: { debug: false, scmToken: `TOKEN`, }, pkg: { repository: `https://github.com/hyper-expanse/semantic-release-github-notifier.git`, }, }; this.plugin = require(`./index`); }); it(`will not post comments during dry run`, function () { this.config.options.dryRun = true; const promise = this.plugin(this.config); return expect(promise).to.be.fulfilled .and.to.eventually.equal(false); }); it(`will reject if a 'pkg' object is not passed in the 'config' argument`, function () { delete this.config.pkg; const promise = this.plugin(this.config); return expect(promise).to.be.rejectedWith(Error, 'This plugin, ' + '`semantic-release-github-notifier`, was not passed the contents of your package\'s ' + '`package.json` file. Please contact the user of this plugin and request that they pass ' + 'the contents of `package.json` to the plugin.'); }); it(`will reject if no repository information in 'pkg' property of 'config'`, function () { delete this.config.pkg.repository; const promise = this.plugin(this.config); return expect(promise) .to.be.rejectedWith(Error); }); it(`will reject if no SCM token provided`, function () { delete this.config.options.scmToken; const promise = this.plugin(this.config); return expect(promise) .to.be.rejectedWith(Error, `No SCM token provided for GitHub.`); }); it(`will reject if a version is not provided`, function () { delete this.config.data.version; const promise = this.plugin(this.config); return expect(promise) .to.be.rejectedWith(Error, `Invalid version provided to 'semantic-release-github-notifier'.`); }); it(`will reject if an invalid version is provided`, function () { this.config.data.version = `a.b.c`; const promise = this.plugin(this.config); return expect(promise) .to.be.rejectedWith(Error, `Invalid version provided to 'semantic-release-github-notifier'.`); }); it(`will reject if monoRepo option isn't passed for monoRepo version`, function () { this.config.data.version = `my-mono-repo@1.0.0`; const promise = this.plugin(this.config); return expect(promise) .to.be.rejectedWith(Error, `Invalid version provided to 'semantic-release-github-notifier'.`); }); it(`will reject if an invalid version for mono repos is provided`, function () { this.config.options.monoRepo = true; this.config.data.version = `my-mono-repo@a.b.c`; const promise = this.plugin(this.config); return expect(promise) .to.be.rejectedWith(Error, `Invalid version provided to 'semantic-release-github-notifier'.`); }); it(`will reject if an incorrect tagSplitter is passed into options`, function () { this.config.options.monoRepo = true; this.config.options.tagSplitter = '#'; this.config.data.version = `my-mono-repo@1.0.0`; const promise = this.plugin(this.config); return expect(promise) .to.be.rejectedWith(Error, `Invalid version provided to 'semantic-release-github-notifier'.`); }); it(`will reject if it fails to create a GitHub comment`, function () { const notesResponse = nock(`https://api.github.com/`, {encodedQueryParams: true}) .post(/.*/) .times(3) .reply(404); const promise = this.plugin(this.config); return promise .catch(function (error) { expect(error).to.an.instanceof(Error) .and.to.have.property(`message`, `Failed to post comment(s) to GitHub.`); notesResponse.done(); }); }); it(`resolves with true on successful GitHub comment`, function () { const notesResponse = generateCommentsResponse(); const promise = this.plugin(this.config); return promise .then(function (result) { expect(result).to.be.true; notesResponse.done(); }); }); describe(`alternative respository URLs`, function () { it(`should succeed with SSH URL - git@github.com`, function () { this.config.pkg.repository = `git@github.com/hyper-expanse/semantic-release-github-notifier.git`; const notesResponse = generateCommentsResponse(); const promise = this.plugin(this.config); return promise .then(function (result) { expect(result).to.be.true; notesResponse.done(); }); }); it(`should succeed with company deployed SSH URL - git@github.company.com`, function () { this.config.pkg.repository = `git@github.company.com/hyper-expanse/semantic-release-github-notifier.git`; const notesResponse = generateCommentsResponse(`https://api.github.company.com/`, `https://github.company.com/`); const promise = this.plugin(this.config); return promise .then(function (result) { expect(result).to.be.true; notesResponse.done(); }); }); it(`should succeed when using insecure HTTP protocol`, function () { this.config.options.insecureApi = true; const notesResponse = generateCommentsResponse(`http://api.github.com/`, `http://github.com/`); const promise = this.plugin(this.config); return promise .then(function (result) { expect(result).to.be.true; notesResponse.done(); }); }); }); }); function generateCommentsResponse(customEndpoint, customUrl) { const endpoint = customEndpoint || `https://api.github.com/`; const url = customUrl || `https://github.com/`; return nock(endpoint, {encodedQueryParams: true}) .post( `/repos/hyper-expanse/semantic-release-github-notifier/issues/1/comments`, `{"body":"Version [1.0.0](${url}hyper-expanse/semantic-release-github-notifier/releases/tag/1.0.0) has been released."}` ).reply(201) .post( `/repos/hyper-expanse/semantic-release-github-notifier/issues/2/comments`, `{"body":"Version [1.0.0](${url}hyper-expanse/semantic-release-github-notifier/releases/tag/1.0.0) has been released."}` ).reply(201) .post( `/repos/hyper-expanse/semantic-release-github-notifier/issues/3/comments`, `{"body":"Version [1.0.0](${url}hyper-expanse/semantic-release-github-notifier/releases/tag/1.0.0) has been released."}` ).reply(201) .post( `/repos/hyper-expanse/semantic-release-github-notifier/issues/4/comments`, `{"body":"Version [1.0.0](${url}hyper-expanse/semantic-release-github-notifier/releases/tag/1.0.0) has been released."}` ).reply(201) .post( `/repos/hyper-expanse/semantic-release-github-notifier/issues/5/comments`, `{"body":"Version [1.0.0](${url}hyper-expanse/semantic-release-github-notifier/releases/tag/1.0.0) has been released."}` ).reply(201) ; }