Skip to content

Commit

Permalink
test: guarantee call order for hooks
Browse files Browse the repository at this point in the history
+ reorder tests

Signed-off-by: Tony Gorez <[email protected]>
  • Loading branch information
tony-go committed May 16, 2024
1 parent 7e40185 commit 40272f2
Showing 1 changed file with 50 additions and 36 deletions.
86 changes: 50 additions & 36 deletions test/AstAnalyser.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,66 +176,80 @@ describe("AstAnalyser", (t) => {
assert.equal(result.warnings.length, 1);
});

describe("intialize", () => {
const analyser = new AstAnalyser();
describe("hooks", () => {
describe("initialize", () => {
const analyser = new AstAnalyser();

it("should throw if initialize is not a function", () => {
assert.throws(() => {
analyser.analyse("const foo = 'bar';", {
initialize: "foo"
it("should throw if initialize is not a function", () => {
assert.throws(() => {
analyser.analyse("const foo = 'bar';", {
initialize: "foo"
});
});
});
});

it("should call the initialize function", (t) => {
const initialize = t.mock.fn();
it("should call the initialize function", (t) => {
const initialize = t.mock.fn();

analyser.analyse("const foo = 'bar';", {
initialize
analyser.analyse("const foo = 'bar';", {
initialize
});

assert.strictEqual(initialize.mock.callCount(), 1);
});

assert.strictEqual(initialize.mock.callCount(), 1);
});
it("should pass the source file as first argument", (t) => {
const initialize = t.mock.fn();

it("should pass the source file as first argument", (t) => {
const initialize = t.mock.fn();
analyser.analyse("const foo = 'bar';", {
initialize
});

analyser.analyse("const foo = 'bar';", {
initialize
assert.strictEqual(initialize.mock.calls[0].arguments[0] instanceof SourceFile, true);
});

assert.strictEqual(initialize.mock.calls[0].arguments[0] instanceof SourceFile, true);
});
});

describe("finalize", () => {
const analyser = new AstAnalyser();
it("should throw if finalize is not a function", () => {
assert.throws(() => {
analyser.analyse("const foo = 'bar';", {
finalize: "foo"
describe("finalize", () => {
const analyser = new AstAnalyser();
it("should throw if finalize is not a function", () => {
assert.throws(() => {
analyser.analyse("const foo = 'bar';", {
finalize: "foo"
});
});
});
});

it("should call the finalize function", (t) => {
const finalize = t.mock.fn();
it("should call the finalize function", (t) => {
const finalize = t.mock.fn();

analyser.analyse("const foo = 'bar';", {
finalize
analyser.analyse("const foo = 'bar';", {
finalize
});

assert.strictEqual(finalize.mock.callCount(), 1);
});

assert.strictEqual(finalize.mock.callCount(), 1);
it("should pass the source file as first argument", (t) => {
const finalize = t.mock.fn();

analyser.analyse("const foo = 'bar';", {
finalize
});

assert.strictEqual(finalize.mock.calls[0].arguments[0] instanceof SourceFile, true);
});
});

it("should pass the source file as first argument", (t) => {
const finalize = t.mock.fn();
it("intialize should be called before finalize", () => {
const calls = [];
const analyser = new AstAnalyser();

analyser.analyse("const foo = 'bar';", {
finalize
initialize: () => calls.push("initialize"),
finalize: () => calls.push("finalize")
});

assert.strictEqual(finalize.mock.calls[0].arguments[0] instanceof SourceFile, true);
assert.deepEqual(calls, ["initialize", "finalize"]);
});
});
});
Expand Down

0 comments on commit 40272f2

Please sign in to comment.