Skip to content

Commit

Permalink
test: duplicate function in standalone code, it should fail but it do…
Browse files Browse the repository at this point in the history
…es not (#1361)
  • Loading branch information
epoberezkin committed Dec 19, 2020
1 parent 5fe4bc0 commit e446893
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions spec/standalone.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,56 @@ describe("standalone code generation", () => {
}
})

describe.only("two refs to the same schema (issue #1361)", () => {
const userSchema = {
$id: "user.json",
type: "object",
properties: {
name: {type: "string"},
},
required: ["name"],
}

const infoSchema = {
$id: "info.json",
type: "object",
properties: {
author: {$ref: "user.json"},
contributors: {
type: "array",
items: {$ref: "user.json"},
},
},
required: ["author", "contributors"],
}

describe("all exports", () => {
it("should not have duplicate functions", () => {
const ajv = new _Ajv({
allErrors: true,
code: {optimize: false, source: true},
inlineRefs: false, // it is needed to show the issue, schemas with refs won't be inlined anyway
schemas: [userSchema, infoSchema],
})

const moduleCode = standaloneCode(ajv)
console.log(moduleCode)
const {"user.json": validateUser, "info.json": validateInfo} = requireFromString(moduleCode)
assert.strictEqual(validateUser({}), false)
assert.strictEqual(validateUser({name: "usr1"}), true)

assert.strictEqual(validateInfo({}), false)
assert.strictEqual(
validateInfo({
author: {name: "usr1"},
contributors: [{name: "usr2"}],
}),
true
)
})
})
})

it("should generate module code with a single export (ESM compatible)", () => {
const ajv = new _Ajv({code: {source: true}})
const v = ajv.compile({
Expand Down

0 comments on commit e446893

Please sign in to comment.