generated from actions/javascript-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validation.test.js
50 lines (40 loc) · 1.41 KB
/
validation.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
const path = require('path')
const fs = require('fs')
const yaml = require('yaml')
const { configParse } = require('./utils/config-helper')
describe('Yaml validation against Json schema', () => {
let yamlFilePath
let schemaFilePath
let yamlData
beforeAll(() => {
yamlFilePath = path.join(__dirname, '/fixtures/github/make_dispatches.yaml')
schemaFilePath = path.join(__dirname, 'schema/jsonschema.json')
const yamlContent = fs.readFileSync(yamlFilePath, 'utf8')
yamlData = configParse(yamlContent)
})
test('should validate Yaml successfully against the Json Schema', () => {
expect(yamlData).toBeDefined()
})
test('should fail if Yaml data does not match the schema', () => {
const invalidYamlData = {
...yamlData,
dispatches: [...yamlData.dispatches]
}
invalidYamlData.dispatches[0].extraField = 'invalid'
expect(() => configParse(yaml.stringify(invalidYamlData))).toThrow()
})
test('should fail if a required field is missing in Yaml', () => {
const yamlWithoutRequiredField = {
...yamlData,
dispatches: [...yamlData.dispatches]
}
yamlWithoutRequiredField.dispatches[0].state_repos =
yamlWithoutRequiredField.dispatches[0].state_repos.map(repo => {
const { repo: removedRepo, ...rest } = repo
return rest
})
expect(() =>
configParse(yaml.stringify(yamlWithoutRequiredField))
).toThrow()
})
})