UNPKG

4.47 kBJavaScriptView Raw
1var io = require('lerna-semantic-release-io').mocks;
2var expect = require('expect.js');
3var path = require('path');
4var perform = require('../index');
5
6describe('perform', function() {
7 describe('with three pakcages set up', function () {
8 beforeEach(function () {
9 var fsState = {
10 'packages': {
11 'a': {
12 'index.js': 'Modified',
13 'package.json': JSON.stringify({name: 'a', version: '0.0.1'})
14 },
15 'b': {
16 'index.js': 'Modified',
17 'package.json': JSON.stringify({name: 'b', version: '0.0.1'})
18 },
19 'c': {
20 'index.js': 'Unmodified',
21 'package.json': JSON.stringify({name: 'c', version: '0.0.0'})
22 }
23 },
24 'package.json': JSON.stringify({name: 'main', version: '0.0.0'}),
25 'lerna.json': JSON.stringify({lerna: '2.0.0-beta.17', version: 'independent'})
26 };
27
28 io.mock({
29 fs: fsState,
30 git: {
31 allTags: [
32 'a@0.0.0',
33 'b@0.0.0',
34 'c@0.0.0'
35 ],
36 head: 'BAR'
37 },
38 npm: {
39 versions: {
40 'a': '0.0.0',
41 'b': '0.0.0',
42 'c': '0.0.0'
43 }
44 },
45 lerna: {
46 versions: {
47 'a': '0.0.1',
48 'b': '0.0.1',
49 'c': '0.0.0'
50 }
51 }
52 });
53 });
54
55 afterEach(function () {
56 io.restore();
57 });
58
59 describe('when npm publish fails', function () {
60 const oldNpmPublish = io.npm.publish;
61 beforeEach(function (done) {
62 io.npm.publish = function () {
63 return function failNpmPublish(cb) {
64 var error = Object.create(Error.prototype);
65 error.message = 'Mock error for npm publish';
66 cb(error);
67 };
68 };
69
70 perform({
71 io: io,
72 callback: done
73 });
74 });
75
76 afterEach(function() {
77 io.npm.publish = oldNpmPublish;
78 });
79
80 it('does not write to the released packages file', function () {
81 var fileContents = io.fs.readFileSync('./.released-packages').toString();
82 expect(fileContents.trim().length).to.equal(0);
83 });
84 });
85
86 describe('executing perform', function () {
87 beforeEach(function (done) {
88 perform({
89 io: io,
90 callback: done
91 });
92 });
93
94 it('npm publishes twice', function () {
95 expect(io.npm.publish.innerTask.callCount).to.equal(2);
96 });
97
98 it('npm publishes a and b', function () {
99 expect(io.npm.publish.firstCall.args[0]).to.equal('packages/a');
100 expect(io.npm.publish.secondCall.args[0]).to.equal('packages/b');
101 });
102
103 it('writes a file recording published packages', function () {
104 var fileContents = io.fs.readFileSync('./.released-packages').toString().split('\n');
105 expect(fileContents.length).to.equal(2);
106 expect(fileContents[0]).to.equal('a@0.0.1');
107 expect(fileContents[1]).to.equal('b@0.0.1');
108 });
109
110 it('pushes commits', function () {
111 expect(io.git.push.called).to.equal(true);
112 });
113
114 it('pushes tags', function () {
115 expect(io.git.pushTags.called).to.equal(true);
116 });
117 });
118 });
119
120 describe('with a private package set up', function () {
121 beforeEach(function () {
122 var fsState = {
123 'packages': {
124 'private': {
125 'index.js': 'Private!',
126 'package.json': JSON.stringify({name: 'private', version: '0.0.1', 'private': true})
127 }
128 },
129 'package.json': JSON.stringify({name: 'main', version: '0.0.0'}),
130 'lerna.json': JSON.stringify({lerna: '2.0.0-beta.17', version: 'independent'})
131 };
132
133 io.mock({
134 fs: fsState,
135 git: {
136 allTags: [
137 'private@0.0.0'
138 ],
139 head: 'BAR'
140 },
141 npm: {
142 versions: {
143 'private': '0.0.0'
144 }
145 },
146 lerna: {
147 versions: {
148 'private': '0.0.1'
149 }
150 }
151 });
152 });
153
154 afterEach(function () {
155 io.restore();
156 });
157
158 describe('executing perform', function () {
159 beforeEach(function (done) {
160 perform({
161 io: io,
162 callback: done
163 });
164 });
165
166 it('npm publish is not called', function () {
167 expect(io.npm.publish.innerTask.callCount).to.equal(0);
168 });
169 });
170
171 });
172});