From 83da9dccced89bf8b61f2bd4ad28ffef1a73ded1 Mon Sep 17 00:00:00 2001 From: Austin Fahsl Date: Wed, 27 Jul 2022 16:30:49 -0400 Subject: [PATCH 1/5] fix(create): test script no longer errors with nonsense message --- .../__snapshots__/create-command.test.js.snap | 50 ++++++++++--------- .../create/__tests__/create-command.test.js | 16 ++++++ commands/create/index.js | 18 +++---- commands/create/lib/lerna-module-data.js | 6 ++- 4 files changed, 56 insertions(+), 34 deletions(-) diff --git a/commands/create/__tests__/__snapshots__/create-command.test.js.snap b/commands/create/__tests__/__snapshots__/create-command.test.js.snap index 14659d46c4..6b953d87f1 100644 --- a/commands/create/__tests__/__snapshots__/create-command.test.js.snap +++ b/commands/create/__tests__/__snapshots__/create-command.test.js.snap @@ -43,10 +43,10 @@ index SHA..SHA +'use strict'; + +const myCli = require('..'); ++const assert = require('node:assert').strict; + -+describe('my-cli', () => { -+ it('needs tests'); -+}); ++assert.strictEqual(myCli(), 'Hello from myCli'); ++console.info("myCli tests passed"); diff --git a/packages/my-cli/bin/my-cli b/packages/my-cli/bin/my-cli new file mode 100755 index SHA..SHA @@ -102,7 +102,7 @@ index SHA..SHA +module.exports = myCli; + +function myCli() { -+ // TODO ++ return "Hello from myCli"; +} diff --git a/packages/my-cli/package.json b/packages/my-cli/package.json new file mode 100644 @@ -134,7 +134,7 @@ index SHA..SHA + "url": "git+https://github.com/test/test.git" + }, + "scripts": { -+ "test": "echo \\"Error: run tests from root\\" && exit 1" ++ "test": "node \\"./__tests__/my-cli.test.js\\"" + }, + "dependencies": { + "yargs": "^1.0.0-mocked" @@ -184,11 +184,11 @@ index SHA..SHA --- /dev/null +++ b/packages/my-es-cli/__tests__/my-es-cli.test.js @@ -0,0 +1,5 @@ -+import myEsCli from '../src/my-es-cli'; ++import myEsCli from '../src/my-es-cli.js'; ++import { strict as assert } from 'node:assert'; + -+describe('my-es-cli', () => { -+ it('needs tests'); -+}); ++assert.strictEqual(myEsCli(), 'Hello from myEsCli'); ++console.info("myEsCli tests passed"); diff --git a/packages/my-es-cli/bin/my-es-cli b/packages/my-es-cli/bin/my-es-cli new file mode 100755 index SHA..SHA @@ -206,7 +206,7 @@ new file mode 100644 index SHA..SHA --- /dev/null +++ b/packages/my-es-cli/package.json -@@ -0,0 +1,35 @@ +@@ -0,0 +1,36 @@ +{ + "name": "my-es-cli", + "version": "1.2.3", @@ -232,11 +232,12 @@ index SHA..SHA + "url": "git+https://github.com/test/test.git" + }, + "scripts": { -+ "test": "echo \\"Error: run tests from root\\" && exit 1" ++ "test": "node \\"./__tests__/my-es-cli.test.js\\"" + }, + "dependencies": { + "yargs": "^1.0.0-mocked" + }, ++ "type": "module", + "bugs": { + "url": "https://github.com/test/test/issues" + }, @@ -279,7 +280,7 @@ index SHA..SHA +++ b/packages/my-es-cli/src/my-es-cli.js @@ -0,0 +1,3 @@ +export default function myEsCli() { -+ // TODO ++ return "Hello from myEsCli"; +} `; @@ -310,10 +311,10 @@ index SHA..SHA +'use strict'; + +const myPkg = require('..'); ++const assert = require('node:assert').strict; + -+describe('my-pkg', () => { -+ it('needs tests'); -+}); ++assert.strictEqual(myPkg(), 'Hello from myPkg'); ++console.info("myPkg tests passed"); diff --git a/packages/my-pkg/lib/my-pkg.js b/packages/my-pkg/lib/my-pkg.js new file mode 100644 index SHA..SHA @@ -325,7 +326,7 @@ index SHA..SHA +module.exports = myPkg; + +function myPkg() { -+ // TODO ++ return "Hello from myPkg"; +} diff --git a/packages/my-pkg/package.json b/packages/my-pkg/package.json new file mode 100644 @@ -353,7 +354,7 @@ index SHA..SHA + "url": "git+https://github.com/test/test.git" + }, + "scripts": { -+ "test": "echo \\"Error: run tests from root\\" && exit 1" ++ "test": "node \\"./__tests__/my-pkg.test.js\\"" + }, + "bugs": { + "url": "https://github.com/test/test/issues" @@ -386,17 +387,17 @@ index SHA..SHA --- /dev/null +++ b/packages/my-pkg/__tests__/my-pkg.test.js @@ -0,0 +1,5 @@ -+import myPkg from '../src/my-pkg'; ++import myPkg from '../src/my-pkg.js'; ++import { strict as assert } from 'node:assert'; + -+describe('my-pkg', () => { -+ it('needs tests'); -+}); ++assert.strictEqual(myPkg(), 'Hello from myPkg'); ++console.info("myPkg tests passed"); diff --git a/packages/my-pkg/package.json b/packages/my-pkg/package.json new file mode 100644 index SHA..SHA --- /dev/null +++ b/packages/my-pkg/package.json -@@ -0,0 +1,28 @@ +@@ -0,0 +1,29 @@ +{ + "name": "my-pkg", + "version": "1.2.3", @@ -418,8 +419,9 @@ index SHA..SHA + "url": "git+https://github.com/test/test.git" + }, + "scripts": { -+ "test": "echo \\"Error: run tests from root\\" && exit 1" ++ "test": "node \\"./__tests__/my-pkg.test.js\\"" + }, ++ "type": "module", + "bugs": { + "url": "https://github.com/test/test/issues" + }, @@ -432,6 +434,6 @@ index SHA..SHA +++ b/packages/my-pkg/src/my-pkg.js @@ -0,0 +1,3 @@ +export default function myPkg() { -+ // TODO ++ return "Hello from myPkg"; +} `; diff --git a/commands/create/__tests__/create-command.test.js b/commands/create/__tests__/create-command.test.js index 0720f8a9c1..966d4cfa5f 100644 --- a/commands/create/__tests__/create-command.test.js +++ b/commands/create/__tests__/create-command.test.js @@ -356,4 +356,20 @@ describe("CreateCommand", () => { expect(await manifestCreated(cwd)).not.toHaveProperty("repository"); }); + + it("adds type field when using esModule", async () => { + const cwd = await initFixture("basic"); + + await lernaCreate(cwd)("a-pkg", "--es-module"); + + expect(await manifestCreated(cwd)).toHaveProperty("type", "module"); + }); + + it("skips type field when not using esModule", async () => { + const cwd = await initFixture("basic"); + + await lernaCreate(cwd)("a-pkg"); + + expect(await manifestCreated(cwd)).not.toHaveProperty("type"); + }); }); diff --git a/commands/create/index.js b/commands/create/index.js index 7fc888ce2d..1ac054b5af 100644 --- a/commands/create/index.js +++ b/commands/create/index.js @@ -399,7 +399,7 @@ class CreateCommand extends Command { const libContent = this.options.esModule ? dedent` export default function ${this.camelName}() { - // TODO + return "Hello from ${this.camelName}"; } ` : dedent` @@ -408,7 +408,7 @@ class CreateCommand extends Command { module.exports = ${this.camelName}; function ${this.camelName}() { - // TODO + return "Hello from ${this.camelName}"; } `; @@ -418,20 +418,20 @@ class CreateCommand extends Command { writeTestFile() { const testContent = this.options.esModule ? dedent` - import ${this.camelName} from '../src/${this.dirName}'; + import ${this.camelName} from '../src/${this.dirName}.js'; + import { strict as assert } from 'node:assert'; - describe('${this.pkgName}', () => { - it('needs tests'); - }); + assert.strictEqual(${this.camelName}(), 'Hello from ${this.camelName}'); + console.info("${this.camelName} tests passed"); ` : dedent` 'use strict'; const ${this.camelName} = require('..'); + const assert = require('node:assert').strict; - describe('${this.pkgName}', () => { - it('needs tests'); - }); + assert.strictEqual(${this.camelName}(), 'Hello from ${this.camelName}'); + console.info("${this.camelName} tests passed"); `; return catFile(this.testDir, this.testFileName, testContent); diff --git a/commands/create/lib/lerna-module-data.js b/commands/create/lib/lerna-module-data.js index 29afbfb894..d65dcae0f1 100644 --- a/commands/create/lib/lerna-module-data.js +++ b/commands/create/lib/lerna-module-data.js @@ -193,10 +193,14 @@ if (!this.package.repository) { if (!this.package.scripts) { exports.scripts = { - test: 'echo "Error: run tests from root" && exit 1', + test: `node "./__tests__/${name}.test.js"`, }; } if (!this.package.dependencies && this.config.get("dependencies")) { exports.dependencies = this.config.get("dependencies"); } + +if (this.config.get("esModule")) { + exports.type = "module"; +} From d9df13627605a73ee037921cc6f94d62ee70d66a Mon Sep 17 00:00:00 2001 From: Austin Fahsl Date: Wed, 27 Jul 2022 16:40:13 -0400 Subject: [PATCH 2/5] chore(e2e): update diff test based on new scripts --- e2e/tests/lerna-diff/lerna-diff.spec.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/e2e/tests/lerna-diff/lerna-diff.spec.ts b/e2e/tests/lerna-diff/lerna-diff.spec.ts index a6100fdfc9..6f3085e987 100644 --- a/e2e/tests/lerna-diff/lerna-diff.spec.ts +++ b/e2e/tests/lerna-diff/lerna-diff.spec.ts @@ -52,7 +52,7 @@ describe("lerna-diff", () => { @@ -19,5 +19,8 @@ }, "scripts": { - "test": "echo \\"Error: run tests from root\\" && exit 1" + "test": "node \\"./__tests__/package-a.test.js\\"" + }, + "dependencies": { + "package-b": "0.0.0" @@ -65,7 +65,7 @@ describe("lerna-diff", () => { @@ -19,5 +19,8 @@ }, "scripts": { - "test": "echo \\"Error: run tests from root\\" && exit 1" + "test": "node \\"./__tests__/package-b.test.js\\"" + }, + "dependencies": { + "package-a": "0.0.0" @@ -98,7 +98,7 @@ describe("lerna-diff", () => { @@ -19,5 +19,8 @@ }, "scripts": { - "test": "echo \\"Error: run tests from root\\" && exit 1" + "test": "node \\"./__tests__/package-a.test.js\\"" + }, + "dependencies": { + "package-b": "0.0.0" From 5344dbd6abaf5960c70a40df9ffeaa4b460ff545 Mon Sep 17 00:00:00 2001 From: Austin Fahsl Date: Wed, 27 Jul 2022 17:14:24 -0400 Subject: [PATCH 3/5] fix: import from 'assert' to support node@^14.15.0 --- .../__tests__/__snapshots__/create-command.test.js.snap | 8 ++++---- commands/create/index.js | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/commands/create/__tests__/__snapshots__/create-command.test.js.snap b/commands/create/__tests__/__snapshots__/create-command.test.js.snap index 6b953d87f1..7fa6e371c2 100644 --- a/commands/create/__tests__/__snapshots__/create-command.test.js.snap +++ b/commands/create/__tests__/__snapshots__/create-command.test.js.snap @@ -43,7 +43,7 @@ index SHA..SHA +'use strict'; + +const myCli = require('..'); -+const assert = require('node:assert').strict; ++const assert = require('assert').strict; + +assert.strictEqual(myCli(), 'Hello from myCli'); +console.info("myCli tests passed"); @@ -185,7 +185,7 @@ index SHA..SHA +++ b/packages/my-es-cli/__tests__/my-es-cli.test.js @@ -0,0 +1,5 @@ +import myEsCli from '../src/my-es-cli.js'; -+import { strict as assert } from 'node:assert'; ++import { strict as assert } from 'assert'; + +assert.strictEqual(myEsCli(), 'Hello from myEsCli'); +console.info("myEsCli tests passed"); @@ -311,7 +311,7 @@ index SHA..SHA +'use strict'; + +const myPkg = require('..'); -+const assert = require('node:assert').strict; ++const assert = require('assert').strict; + +assert.strictEqual(myPkg(), 'Hello from myPkg'); +console.info("myPkg tests passed"); @@ -388,7 +388,7 @@ index SHA..SHA +++ b/packages/my-pkg/__tests__/my-pkg.test.js @@ -0,0 +1,5 @@ +import myPkg from '../src/my-pkg.js'; -+import { strict as assert } from 'node:assert'; ++import { strict as assert } from 'assert'; + +assert.strictEqual(myPkg(), 'Hello from myPkg'); +console.info("myPkg tests passed"); diff --git a/commands/create/index.js b/commands/create/index.js index 1ac054b5af..ece42d46a3 100644 --- a/commands/create/index.js +++ b/commands/create/index.js @@ -419,7 +419,7 @@ class CreateCommand extends Command { const testContent = this.options.esModule ? dedent` import ${this.camelName} from '../src/${this.dirName}.js'; - import { strict as assert } from 'node:assert'; + import { strict as assert } from 'assert'; assert.strictEqual(${this.camelName}(), 'Hello from ${this.camelName}'); console.info("${this.camelName} tests passed"); @@ -428,7 +428,7 @@ class CreateCommand extends Command { 'use strict'; const ${this.camelName} = require('..'); - const assert = require('node:assert').strict; + const assert = require('assert').strict; assert.strictEqual(${this.camelName}(), 'Hello from ${this.camelName}'); console.info("${this.camelName} tests passed"); From 38cb6c52c699afd71c5da445184838983065c623 Mon Sep 17 00:00:00 2001 From: Austin Fahsl Date: Thu, 28 Jul 2022 15:51:41 -0400 Subject: [PATCH 4/5] chore(create): remove quotes from generated test script --- .../__tests__/__snapshots__/create-command.test.js.snap | 8 ++++---- commands/create/lib/lerna-module-data.js | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/commands/create/__tests__/__snapshots__/create-command.test.js.snap b/commands/create/__tests__/__snapshots__/create-command.test.js.snap index 7fa6e371c2..5cd0776b21 100644 --- a/commands/create/__tests__/__snapshots__/create-command.test.js.snap +++ b/commands/create/__tests__/__snapshots__/create-command.test.js.snap @@ -134,7 +134,7 @@ index SHA..SHA + "url": "git+https://github.com/test/test.git" + }, + "scripts": { -+ "test": "node \\"./__tests__/my-cli.test.js\\"" ++ "test": "node ./__tests__/my-cli.test.js" + }, + "dependencies": { + "yargs": "^1.0.0-mocked" @@ -232,7 +232,7 @@ index SHA..SHA + "url": "git+https://github.com/test/test.git" + }, + "scripts": { -+ "test": "node \\"./__tests__/my-es-cli.test.js\\"" ++ "test": "node ./__tests__/my-es-cli.test.js" + }, + "dependencies": { + "yargs": "^1.0.0-mocked" @@ -354,7 +354,7 @@ index SHA..SHA + "url": "git+https://github.com/test/test.git" + }, + "scripts": { -+ "test": "node \\"./__tests__/my-pkg.test.js\\"" ++ "test": "node ./__tests__/my-pkg.test.js" + }, + "bugs": { + "url": "https://github.com/test/test/issues" @@ -419,7 +419,7 @@ index SHA..SHA + "url": "git+https://github.com/test/test.git" + }, + "scripts": { -+ "test": "node \\"./__tests__/my-pkg.test.js\\"" ++ "test": "node ./__tests__/my-pkg.test.js" + }, + "type": "module", + "bugs": { diff --git a/commands/create/lib/lerna-module-data.js b/commands/create/lib/lerna-module-data.js index d65dcae0f1..a9063d9641 100644 --- a/commands/create/lib/lerna-module-data.js +++ b/commands/create/lib/lerna-module-data.js @@ -193,7 +193,7 @@ if (!this.package.repository) { if (!this.package.scripts) { exports.scripts = { - test: `node "./__tests__/${name}.test.js"`, + test: `node ./__tests__/${name}.test.js`, }; } From 9ba2de48254664b7fa8595ed0856137f82d29559 Mon Sep 17 00:00:00 2001 From: Austin Fahsl Date: Thu, 28 Jul 2022 16:38:51 -0400 Subject: [PATCH 5/5] chore(e2e): remove quotes from expected snapshot --- e2e/tests/lerna-diff/lerna-diff.spec.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/e2e/tests/lerna-diff/lerna-diff.spec.ts b/e2e/tests/lerna-diff/lerna-diff.spec.ts index 6f3085e987..685eb88199 100644 --- a/e2e/tests/lerna-diff/lerna-diff.spec.ts +++ b/e2e/tests/lerna-diff/lerna-diff.spec.ts @@ -52,7 +52,7 @@ describe("lerna-diff", () => { @@ -19,5 +19,8 @@ }, "scripts": { - "test": "node \\"./__tests__/package-a.test.js\\"" + "test": "node ./__tests__/package-a.test.js" + }, + "dependencies": { + "package-b": "0.0.0" @@ -65,7 +65,7 @@ describe("lerna-diff", () => { @@ -19,5 +19,8 @@ }, "scripts": { - "test": "node \\"./__tests__/package-b.test.js\\"" + "test": "node ./__tests__/package-b.test.js" + }, + "dependencies": { + "package-a": "0.0.0" @@ -98,7 +98,7 @@ describe("lerna-diff", () => { @@ -19,5 +19,8 @@ }, "scripts": { - "test": "node \\"./__tests__/package-a.test.js\\"" + "test": "node ./__tests__/package-a.test.js" + }, + "dependencies": { + "package-b": "0.0.0"