Skip to content

Commit dfd30fa

Browse files
authored
fix(create): default test script no longer errors (#3266)
1 parent 1d11378 commit dfd30fa

File tree

5 files changed

+59
-37
lines changed

5 files changed

+59
-37
lines changed

commands/create/__tests__/__snapshots__/create-command.test.js.snap

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ index SHA..SHA
4343
+'use strict';
4444
+
4545
+const myCli = require('..');
46+
+const assert = require('assert').strict;
4647
+
47-
+describe('my-cli', () => {
48-
+ it('needs tests');
49-
+});
48+
+assert.strictEqual(myCli(), 'Hello from myCli');
49+
+console.info("myCli tests passed");
5050
diff --git a/packages/my-cli/bin/my-cli b/packages/my-cli/bin/my-cli
5151
new file mode 100755
5252
index SHA..SHA
@@ -102,7 +102,7 @@ index SHA..SHA
102102
+module.exports = myCli;
103103
+
104104
+function myCli() {
105-
+ // TODO
105+
+ return "Hello from myCli";
106106
+}
107107
diff --git a/packages/my-cli/package.json b/packages/my-cli/package.json
108108
new file mode 100644
@@ -134,7 +134,7 @@ index SHA..SHA
134134
+ "url": "git+https://github.com/test/test.git"
135135
+ },
136136
+ "scripts": {
137-
+ "test": "echo \\"Error: run tests from root\\" && exit 1"
137+
+ "test": "node ./__tests__/my-cli.test.js"
138138
+ },
139139
+ "dependencies": {
140140
+ "yargs": "^1.0.0-mocked"
@@ -184,11 +184,11 @@ index SHA..SHA
184184
--- /dev/null
185185
+++ b/packages/my-es-cli/__tests__/my-es-cli.test.js
186186
@@ -0,0 +1,5 @@
187-
+import myEsCli from '../src/my-es-cli';
187+
+import myEsCli from '../src/my-es-cli.js';
188+
+import { strict as assert } from 'assert';
188189
+
189-
+describe('my-es-cli', () => {
190-
+ it('needs tests');
191-
+});
190+
+assert.strictEqual(myEsCli(), 'Hello from myEsCli');
191+
+console.info("myEsCli tests passed");
192192
diff --git a/packages/my-es-cli/bin/my-es-cli b/packages/my-es-cli/bin/my-es-cli
193193
new file mode 100755
194194
index SHA..SHA
@@ -206,7 +206,7 @@ new file mode 100644
206206
index SHA..SHA
207207
--- /dev/null
208208
+++ b/packages/my-es-cli/package.json
209-
@@ -0,0 +1,35 @@
209+
@@ -0,0 +1,36 @@
210210
+{
211211
+ "name": "my-es-cli",
212212
+ "version": "1.2.3",
@@ -232,11 +232,12 @@ index SHA..SHA
232232
+ "url": "git+https://github.com/test/test.git"
233233
+ },
234234
+ "scripts": {
235-
+ "test": "echo \\"Error: run tests from root\\" && exit 1"
235+
+ "test": "node ./__tests__/my-es-cli.test.js"
236236
+ },
237237
+ "dependencies": {
238238
+ "yargs": "^1.0.0-mocked"
239239
+ },
240+
+ "type": "module",
240241
+ "bugs": {
241242
+ "url": "https://github.com/test/test/issues"
242243
+ },
@@ -279,7 +280,7 @@ index SHA..SHA
279280
+++ b/packages/my-es-cli/src/my-es-cli.js
280281
@@ -0,0 +1,3 @@
281282
+export default function myEsCli() {
282-
+ // TODO
283+
+ return "Hello from myEsCli";
283284
+}
284285
`;
285286

@@ -310,10 +311,10 @@ index SHA..SHA
310311
+'use strict';
311312
+
312313
+const myPkg = require('..');
314+
+const assert = require('assert').strict;
313315
+
314-
+describe('my-pkg', () => {
315-
+ it('needs tests');
316-
+});
316+
+assert.strictEqual(myPkg(), 'Hello from myPkg');
317+
+console.info("myPkg tests passed");
317318
diff --git a/packages/my-pkg/lib/my-pkg.js b/packages/my-pkg/lib/my-pkg.js
318319
new file mode 100644
319320
index SHA..SHA
@@ -325,7 +326,7 @@ index SHA..SHA
325326
+module.exports = myPkg;
326327
+
327328
+function myPkg() {
328-
+ // TODO
329+
+ return "Hello from myPkg";
329330
+}
330331
diff --git a/packages/my-pkg/package.json b/packages/my-pkg/package.json
331332
new file mode 100644
@@ -353,7 +354,7 @@ index SHA..SHA
353354
+ "url": "git+https://github.com/test/test.git"
354355
+ },
355356
+ "scripts": {
356-
+ "test": "echo \\"Error: run tests from root\\" && exit 1"
357+
+ "test": "node ./__tests__/my-pkg.test.js"
357358
+ },
358359
+ "bugs": {
359360
+ "url": "https://github.com/test/test/issues"
@@ -386,17 +387,17 @@ index SHA..SHA
386387
--- /dev/null
387388
+++ b/packages/my-pkg/__tests__/my-pkg.test.js
388389
@@ -0,0 +1,5 @@
389-
+import myPkg from '../src/my-pkg';
390+
+import myPkg from '../src/my-pkg.js';
391+
+import { strict as assert } from 'assert';
390392
+
391-
+describe('my-pkg', () => {
392-
+ it('needs tests');
393-
+});
393+
+assert.strictEqual(myPkg(), 'Hello from myPkg');
394+
+console.info("myPkg tests passed");
394395
diff --git a/packages/my-pkg/package.json b/packages/my-pkg/package.json
395396
new file mode 100644
396397
index SHA..SHA
397398
--- /dev/null
398399
+++ b/packages/my-pkg/package.json
399-
@@ -0,0 +1,28 @@
400+
@@ -0,0 +1,29 @@
400401
+{
401402
+ "name": "my-pkg",
402403
+ "version": "1.2.3",
@@ -418,8 +419,9 @@ index SHA..SHA
418419
+ "url": "git+https://github.com/test/test.git"
419420
+ },
420421
+ "scripts": {
421-
+ "test": "echo \\"Error: run tests from root\\" && exit 1"
422+
+ "test": "node ./__tests__/my-pkg.test.js"
422423
+ },
424+
+ "type": "module",
423425
+ "bugs": {
424426
+ "url": "https://github.com/test/test/issues"
425427
+ },
@@ -432,6 +434,6 @@ index SHA..SHA
432434
+++ b/packages/my-pkg/src/my-pkg.js
433435
@@ -0,0 +1,3 @@
434436
+export default function myPkg() {
435-
+ // TODO
437+
+ return "Hello from myPkg";
436438
+}
437439
`;

commands/create/__tests__/create-command.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,4 +356,20 @@ describe("CreateCommand", () => {
356356

357357
expect(await manifestCreated(cwd)).not.toHaveProperty("repository");
358358
});
359+
360+
it("adds type field when using esModule", async () => {
361+
const cwd = await initFixture("basic");
362+
363+
await lernaCreate(cwd)("a-pkg", "--es-module");
364+
365+
expect(await manifestCreated(cwd)).toHaveProperty("type", "module");
366+
});
367+
368+
it("skips type field when not using esModule", async () => {
369+
const cwd = await initFixture("basic");
370+
371+
await lernaCreate(cwd)("a-pkg");
372+
373+
expect(await manifestCreated(cwd)).not.toHaveProperty("type");
374+
});
359375
});

commands/create/index.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ class CreateCommand extends Command {
399399
const libContent = this.options.esModule
400400
? dedent`
401401
export default function ${this.camelName}() {
402-
// TODO
402+
return "Hello from ${this.camelName}";
403403
}
404404
`
405405
: dedent`
@@ -408,7 +408,7 @@ class CreateCommand extends Command {
408408
module.exports = ${this.camelName};
409409
410410
function ${this.camelName}() {
411-
// TODO
411+
return "Hello from ${this.camelName}";
412412
}
413413
`;
414414

@@ -418,20 +418,20 @@ class CreateCommand extends Command {
418418
writeTestFile() {
419419
const testContent = this.options.esModule
420420
? dedent`
421-
import ${this.camelName} from '../src/${this.dirName}';
421+
import ${this.camelName} from '../src/${this.dirName}.js';
422+
import { strict as assert } from 'assert';
422423
423-
describe('${this.pkgName}', () => {
424-
it('needs tests');
425-
});
424+
assert.strictEqual(${this.camelName}(), 'Hello from ${this.camelName}');
425+
console.info("${this.camelName} tests passed");
426426
`
427427
: dedent`
428428
'use strict';
429429
430430
const ${this.camelName} = require('..');
431+
const assert = require('assert').strict;
431432
432-
describe('${this.pkgName}', () => {
433-
it('needs tests');
434-
});
433+
assert.strictEqual(${this.camelName}(), 'Hello from ${this.camelName}');
434+
console.info("${this.camelName} tests passed");
435435
`;
436436

437437
return catFile(this.testDir, this.testFileName, testContent);

commands/create/lib/lerna-module-data.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,14 @@ if (!this.package.repository) {
193193

194194
if (!this.package.scripts) {
195195
exports.scripts = {
196-
test: 'echo "Error: run tests from root" && exit 1',
196+
test: `node ./__tests__/${name}.test.js`,
197197
};
198198
}
199199

200200
if (!this.package.dependencies && this.config.get("dependencies")) {
201201
exports.dependencies = this.config.get("dependencies");
202202
}
203+
204+
if (this.config.get("esModule")) {
205+
exports.type = "module";
206+
}

e2e/tests/lerna-diff/lerna-diff.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ describe("lerna-diff", () => {
5252
@@ -19,5 +19,8 @@
5353
},
5454
"scripts": {
55-
"test": "echo \\"Error: run tests from root\\" && exit 1"
55+
"test": "node ./__tests__/package-a.test.js"
5656
+ },
5757
+ "dependencies": {
5858
+ "package-b": "0.0.0"
@@ -65,7 +65,7 @@ describe("lerna-diff", () => {
6565
@@ -19,5 +19,8 @@
6666
},
6767
"scripts": {
68-
"test": "echo \\"Error: run tests from root\\" && exit 1"
68+
"test": "node ./__tests__/package-b.test.js"
6969
+ },
7070
+ "dependencies": {
7171
+ "package-a": "0.0.0"
@@ -98,7 +98,7 @@ describe("lerna-diff", () => {
9898
@@ -19,5 +19,8 @@
9999
},
100100
"scripts": {
101-
"test": "echo \\"Error: run tests from root\\" && exit 1"
101+
"test": "node ./__tests__/package-a.test.js"
102102
+ },
103103
+ "dependencies": {
104104
+ "package-b": "0.0.0"

0 commit comments

Comments
 (0)