From 7860d98e7a6c291e45dabd13d9939ad17aae432d Mon Sep 17 00:00:00 2001
From: NullVoxPopuli <199018+NullVoxPopuli@users.noreply.github.com>
Date: Tue, 2 Jan 2024 17:16:30 -0500
Subject: [PATCH] Favor ESM tests, ensure CJS still works.
---
package.json | 2 +-
test/{node-api.cjs => parse.test.js} | 93 ++--------------------
test/{node-api-esm.mjs => process.test.js} | 41 ++++++++--
test/require.test.cjs | 52 ++++++++++++
4 files changed, 92 insertions(+), 96 deletions(-)
rename test/{node-api.cjs => parse.test.js} (58%)
rename test/{node-api-esm.mjs => process.test.js} (64%)
create mode 100644 test/require.test.cjs
diff --git a/package.json b/package.json
index 137b627..e94792a 100644
--- a/package.json
+++ b/package.json
@@ -31,7 +31,7 @@
"scripts": {
"build": "./build.sh",
"prepack": "./build.sh",
- "ci:node": "mocha",
+ "ci:node": "mocha 'test/*.{js,cjs}'",
"lint": "concurrently \"npm:lint:*(!fix)\" --names \"lint:\"",
"lint:package": "publint",
"lint:published-types": "attw --pack --ignore-rules false-esm",
diff --git a/test/node-api.cjs b/test/parse.test.js
similarity index 58%
rename from test/node-api.cjs
rename to test/parse.test.js
index 0b8c013..0e0c716 100644
--- a/test/node-api.cjs
+++ b/test/parse.test.js
@@ -1,6 +1,6 @@
-const { Preprocessor } = require("content-tag");
-const chai = require("chai");
-const { codeEquality } = require("code-equality-assertions/chai");
+import chai from "chai";
+import { codeEquality } from "code-equality-assertions/chai";
+import { Preprocessor } from "content-tag";
chai.use(codeEquality);
@@ -8,7 +8,7 @@ const { expect } = chai;
const p = new Preprocessor();
-describe("parse", function () {
+describe(`parse`, function () {
it("basic example", function () {
let output = p.parse("Hello!");
@@ -173,91 +173,8 @@ describe("parse", function () {
p.process(
`const thing = "face";
Hi`,
- "path/to/my/component.gjs"
+ "path/to/my/component.gjs",
);
}).to.throw(`Parse Error at path/to/my/component.gjs:2:15: 2:15`);
});
});
-
-describe("process", function () {
- it("works for a basic example", function () {
- let output = p.process("Hi");
-
- expect(output).to
- .equalCode(`import { template } from "@ember/template-compiler";
- export default template(\`Hi\`, {
- eval () {
- return eval(arguments[0]);
- }
- });`);
- });
-
- it("escapes backticks", function () {
- let input = `
- class Foo extends Component {
- greeting = 'Hello';
-
- {{this.greeting}}, \`lifeform\`!
- }
- `;
-
- let output = p.process(input);
-
- expect(output).to.equalCode(
- `import { template } from "@ember/template-compiler";
- let Foo = class Foo extends Component {
- greeting = 'Hello';
- static{
- template(\`{{this.greeting}}, \\\`lifeform\\\`!\`, {
- component: this,
- eval () {
- return eval(arguments[0]);
- }
- });
- }
- };`
- );
- });
-
- it("Emits parse errors with anonymous file", function () {
- expect(function () {
- p.process(`const thing = "face";
- Hi`);
- }).to.throw(`Parse Error at :2:15: 2:15`);
- });
-
- it("Emits parse errors with real file", function () {
- expect(function () {
- p.process(
- `const thing = "face";
- Hi`,
- "path/to/my/component.gjs"
- );
- }).to.throw(`Parse Error at path/to/my/component.gjs:2:15: 2:15`);
- });
-
- it("Offers source_code snippet on parse errors", function () {
- let parseError;
- try {
- p.process(`class {`);
- } catch (err) {
- parseError = err;
- }
- expect(parseError)
- .to.have.property("source_code")
- .matches(/Expected ident.*class \{/s);
- });
-
- it("Offers source_code_color snippet on parse errors", function () {
- let parseError;
- try {
- p.process(`class {`);
- } catch (err) {
- parseError = err;
- }
- // eslint-disable-next-line no-control-regex
- expect(parseError)
- .to.have.property("source_code_color")
- .matches(/Expected ident.*[\u001b].*class \{/s);
- });
-});
diff --git a/test/node-api-esm.mjs b/test/process.test.js
similarity index 64%
rename from test/node-api-esm.mjs
rename to test/process.test.js
index 55a3c02..c408810 100644
--- a/test/node-api-esm.mjs
+++ b/test/process.test.js
@@ -1,24 +1,51 @@
import chai from "chai";
import { codeEquality } from "code-equality-assertions/chai";
+import { Preprocessor } from "content-tag";
chai.use(codeEquality);
const { expect } = chai;
-import { Preprocessor } from "content-tag";
const p = new Preprocessor();
-describe("Node ESM", function () {
+describe(`process`, function () {
it("works for a basic example", function () {
let output = p.process("Hi");
expect(output).to
.equalCode(`import { template } from "@ember/template-compiler";
-export default template(\`Hi\`, {
- eval () {
- return eval(arguments[0]);
+ export default template(\`Hi\`, {
+ eval () {
+ return eval(arguments[0]);
+ }
+ });`);
+ });
+
+ it("escapes backticks", function () {
+ let input = `
+ class Foo extends Component {
+ greeting = 'Hello';
+
+ {{this.greeting}}, \`lifeform\`!
}
-});`);
+ `;
+
+ let output = p.process(input);
+
+ expect(output).to.equalCode(
+ `import { template } from "@ember/template-compiler";
+ let Foo = class Foo extends Component {
+ greeting = 'Hello';
+ static{
+ template(\`{{this.greeting}}, \\\`lifeform\\\`!\`, {
+ component: this,
+ eval () {
+ return eval(arguments[0]);
+ }
+ });
+ }
+ };`,
+ );
});
it("Emits parse errors with anonymous file", function () {
@@ -33,7 +60,7 @@ export default template(\`Hi\`, {
p.process(
`const thing = "face";
Hi`,
- "path/to/my/component.gjs"
+ "path/to/my/component.gjs",
);
}).to.throw(`Parse Error at path/to/my/component.gjs:2:15: 2:15`);
});
diff --git a/test/require.test.cjs b/test/require.test.cjs
new file mode 100644
index 0000000..efdf9ba
--- /dev/null
+++ b/test/require.test.cjs
@@ -0,0 +1,52 @@
+const chai = require("chai");
+const { codeEquality } = require("code-equality-assertions/chai");
+
+chai.use(codeEquality);
+
+const { expect } = chai;
+
+const { Preprocessor } = require("content-tag");
+
+const p = new Preprocessor();
+
+describe("cjs/require", function () {
+ it("can call process", function () {
+ let output = p.process("Hi");
+
+ expect(output).to
+ .equalCode(`import { template } from "@ember/template-compiler";
+ export default template(\`Hi\`, {
+ eval () {
+ return eval(arguments[0]);
+ }
+ });`);
+ });
+
+ it("can call parse", function () {
+ let output = p.parse("Hello!");
+
+ expect(output).to.eql([
+ {
+ type: "expression",
+ tagName: "template",
+ contents: "Hello!",
+ range: {
+ start: 0,
+ end: 27,
+ },
+ contentRange: {
+ start: 10,
+ end: 16,
+ },
+ startRange: {
+ end: 10,
+ start: 0,
+ },
+ endRange: {
+ start: 16,
+ end: 27,
+ },
+ },
+ ]);
+ });
+});