Skip to content

Commit

Permalink
Favor ESM tests, ensure CJS still works.
Browse files Browse the repository at this point in the history
  • Loading branch information
NullVoxPopuli committed Jan 22, 2024
1 parent 384c86b commit 7860d98
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 96 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
93 changes: 5 additions & 88 deletions test/node-api.cjs → test/parse.test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
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);

const { expect } = chai;

const p = new Preprocessor();

describe("parse", function () {
describe(`parse`, function () {
it("basic example", function () {
let output = p.parse("<template>Hello!</template>");

Expand Down Expand Up @@ -173,91 +173,8 @@ describe("parse", function () {
p.process(
`const thing = "face";
<template>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("<template>Hi</template>");

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';
<template>{{this.greeting}}, \`lifeform\`!</template>
}
`;

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";
<template>Hi`);
}).to.throw(`Parse Error at <anon>:2:15: 2:15`);
});

it("Emits parse errors with real file", function () {
expect(function () {
p.process(
`const thing = "face";
<template>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);
});
});
41 changes: 34 additions & 7 deletions test/node-api-esm.mjs → test/process.test.js
Original file line number Diff line number Diff line change
@@ -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("<template>Hi</template>");

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';
<template>{{this.greeting}}, \`lifeform\`!</template>
}
});`);
`;

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 () {
Expand All @@ -33,7 +60,7 @@ export default template(\`Hi\`, {
p.process(
`const thing = "face";
<template>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`);
});
Expand Down
52 changes: 52 additions & 0 deletions test/require.test.cjs
Original file line number Diff line number Diff line change
@@ -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("<template>Hi</template>");

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("<template>Hello!</template>");

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,
},
},
]);
});
});

0 comments on commit 7860d98

Please sign in to comment.