Skip to content

Commit

Permalink
fix(create): default test script no longer errors (#3266)
Browse files Browse the repository at this point in the history
  • Loading branch information
fahslaj committed Jul 28, 2022
1 parent 1d11378 commit dfd30fa
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 37 deletions.
50 changes: 26 additions & 24 deletions commands/create/__tests__/__snapshots__/create-command.test.js.snap
Expand Up @@ -43,10 +43,10 @@ index SHA..SHA
+'use strict';
+
+const myCli = require('..');
+const assert = require('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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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 '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
Expand All @@ -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",
Expand All @@ -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"
+ },
Expand Down Expand Up @@ -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";
+}
`;

Expand Down Expand Up @@ -310,10 +311,10 @@ index SHA..SHA
+'use strict';
+
+const myPkg = require('..');
+const assert = require('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
Expand All @@ -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
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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 '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",
Expand All @@ -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"
+ },
Expand All @@ -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";
+}
`;
16 changes: 16 additions & 0 deletions commands/create/__tests__/create-command.test.js
Expand Up @@ -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");
});
});
18 changes: 9 additions & 9 deletions commands/create/index.js
Expand Up @@ -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`
Expand All @@ -408,7 +408,7 @@ class CreateCommand extends Command {
module.exports = ${this.camelName};
function ${this.camelName}() {
// TODO
return "Hello from ${this.camelName}";
}
`;

Expand All @@ -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 '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('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);
Expand Down
6 changes: 5 additions & 1 deletion commands/create/lib/lerna-module-data.js
Expand Up @@ -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";
}
6 changes: 3 additions & 3 deletions e2e/tests/lerna-diff/lerna-diff.spec.ts
Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit dfd30fa

Please sign in to comment.