-
-
Notifications
You must be signed in to change notification settings - Fork 680
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: write tests and refactor scripts #3012
Merged
Merged
Changes from 19 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
9749110
refactored scripts, added jest and wrote test for it
vishvamsinh28 4928ac6
added coverage to gitignore
vishvamsinh28 cd3a6cc
fix package-lock
anshgoyalevil 42ce7d7
write json and test updated
vishvamsinh28 8bf5af9
utils test updated
vishvamsinh28 3001124
write json tests updated and fixtures added
vishvamsinh28 b90f0bf
Merge branch 'master' into jestTests
vishvamsinh28 7a2a340
build fix
vishvamsinh28 628daab
Merge branch 'jestTests' of https://github.com/vishvamsinh28/website …
vishvamsinh28 95ca590
Merge branch 'master' into jestTests
anshgoyalevil 4468fc5
test updated
vishvamsinh28 b27a095
fix utils.test.js
anshgoyalevil c818765
Merge branch 'master' into jestTests
anshgoyalevil 6673681
Update tests/utils.test.js
anshgoyalevil afd7eeb
name change
vishvamsinh28 af63f97
updated tests for errors
vishvamsinh28 3bfc5ed
Merge branch 'master' into jestTests
anshgoyalevil 51522e6
Merge branch 'master' into jestTests
akshatnema 57cc6a1
reverted package-lock.json
akshatnema 02faf33
fix package.json
anshgoyalevil File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
module.exports = { | ||
verbose: true, // display individual test results with the test suite hierarchy | ||
collectCoverage: true, // collect test coverage information\ | ||
collectCoverageFrom: ['scripts/**/*.js'] | ||
}; | ||
verbose: true, // display individual test results with the test suite hierarchy | ||
collectCoverage: true, // collect test coverage information\ | ||
collectCoverageFrom: ['scripts/**/*.js'] | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,6 @@ | ||
const { promises: { readFile, writeFile } } = require('fs'); | ||
const { convertToJson } = require('../utils'); | ||
const { resolve } = require('path'); | ||
const writeJSON = require('../utils/readAndWriteJson.js') | ||
|
||
module.exports = async function buildAdoptersList() { | ||
try { | ||
const AdoptersContent = await readFile('config/adopters.yml', 'utf-8'); | ||
const jsonContent = convertToJson(AdoptersContent); | ||
|
||
await writeFile( | ||
resolve(__dirname, '../../config', 'adopters.json'), | ||
JSON.stringify(jsonContent) | ||
); | ||
} catch (err) { | ||
console.error(err); | ||
throw err; | ||
} | ||
writeJSON('config/adopters.yml',resolve(__dirname, '../../config', 'adopters.json')); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
const { promises: { readFile, writeFile } } = require('fs'); | ||
const { convertToJson } = require("../utils"); | ||
|
||
module.exports = async function writeJSON(readPath, writePath) { | ||
let readContent; | ||
let jsonContent; | ||
|
||
// Attempt to read the file | ||
try { | ||
readContent = await readFile(readPath, 'utf-8'); | ||
} catch (err) { | ||
throw new Error(`Error while reading file\nError: ${err}`); | ||
} | ||
|
||
// Attempt to convert content to JSON | ||
try { | ||
jsonContent = convertToJson(readContent); | ||
} catch (err) { | ||
throw new Error(`Error while conversion\nError: ${err}`); | ||
} | ||
|
||
// Attempt to write the JSON content to file | ||
try { | ||
await writeFile(writePath, JSON.stringify(jsonContent)); | ||
} catch (err) { | ||
throw new Error(`Error while writing file\nError: ${err}`); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
const { resolve } = require('path'); | ||
const writeJSON = require('../../scripts/utils/readAndWriteJson.js'); | ||
const buildAdoptersList = require('../../scripts/adopters/index'); | ||
|
||
jest.mock('../../scripts/utils/readAndWriteJson.js'); | ||
|
||
describe('buildAdoptersList', () => { | ||
|
||
test('should call writeJSON with correct arguments', async () => { | ||
const expectedReadPath = 'config/adopters.yml'; | ||
const expectedWritePath = resolve(__dirname, '../../config', 'adopters.json'); | ||
|
||
await buildAdoptersList(); | ||
|
||
expect(writeJSON).toHaveBeenCalledWith(expectedReadPath, expectedWritePath); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module.exports = { | ||
jsonString: '{"name": "AsyncAPI", "age": 5}', | ||
yamlString: 'name: AsyncAPI\nage: 5', | ||
jsonObject: { name: "AsyncAPI", age: 5 }, | ||
invalidString: 'name: AsyncAPI, age: five' | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
const { promises: fs } = require('fs'); | ||
const { convertToJson } = require('../scripts/utils'); | ||
const writeJSON = require("../scripts/utils/readAndWriteJson"); | ||
const { yamlString, jsonObject } = require("./fixtures/utilsData"); | ||
|
||
jest.mock('fs', () => ({ | ||
promises: { | ||
readFile: jest.fn(), | ||
writeFile: jest.fn(), | ||
}, | ||
})); | ||
|
||
jest.mock('../scripts/utils', () => ({ | ||
convertToJson: jest.fn(), | ||
})); | ||
|
||
describe('writeJSON', () => { | ||
const readPath = 'config/testInput.yaml'; | ||
const writePath = 'config/testOutput.json'; | ||
const error = new Error('Test error'); | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
test('should read a file, convert it to JSON, and write the JSON to another file', async () => { | ||
fs.readFile.mockResolvedValue(yamlString); | ||
convertToJson.mockReturnValue(jsonObject); | ||
|
||
await writeJSON(readPath, writePath); | ||
|
||
expect(fs.readFile).toHaveBeenCalledWith(readPath, 'utf-8'); | ||
expect(convertToJson).toHaveBeenCalledWith(yamlString); | ||
expect(fs.writeFile).toHaveBeenCalledWith(writePath, JSON.stringify(jsonObject)); | ||
}); | ||
|
||
test('should throw an error if reading the file fails', async () => { | ||
fs.readFile.mockRejectedValue(error); | ||
|
||
try { | ||
await writeJSON(readPath, writePath); | ||
} catch (err) { | ||
expect(err.message).toBe(`Error while reading file\nError: ${error}`); | ||
} | ||
|
||
expect(fs.readFile).toHaveBeenCalledWith(readPath, 'utf-8'); | ||
}); | ||
|
||
test('should throw an error if converting the file content to JSON fails', async () => { | ||
fs.readFile.mockResolvedValue(yamlString); | ||
convertToJson.mockImplementation(() => { | ||
throw error; | ||
}); | ||
|
||
try { | ||
await writeJSON(readPath, writePath); | ||
} catch (err) { | ||
expect(err.message).toBe(`Error while conversion\nError: ${error}`); | ||
} | ||
|
||
expect(convertToJson).toHaveBeenCalledWith(yamlString); | ||
}); | ||
|
||
test('should throw an error if writing the file fails', async () => { | ||
fs.readFile.mockResolvedValue(yamlString); | ||
convertToJson.mockReturnValue(jsonObject); | ||
fs.writeFile.mockRejectedValue(error); | ||
|
||
try { | ||
await writeJSON(readPath, writePath); | ||
} catch (err) { | ||
expect(err.message).toBe(`Error while writing file\nError: ${error}`); | ||
} | ||
|
||
expect(fs.writeFile).toHaveBeenCalledWith(writePath, JSON.stringify(jsonObject)); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
const { convertToJson } = require("../scripts/utils"); | ||
const { jsonString, yamlString, jsonObject, invalidString } = require("./fixtures/utilsData") | ||
|
||
describe('convertToJson', () => { | ||
test('should return JSON object if input is valid JSON string', () => { | ||
expect(convertToJson(jsonString)).toEqual(jsonObject); | ||
}); | ||
|
||
test('should return JavaScript object if input is valid YAML string', () => { | ||
expect(convertToJson(yamlString)).toEqual(jsonObject); | ||
}); | ||
|
||
test('should return input if input is already an object', () => { | ||
expect(convertToJson(jsonObject)).toBe(jsonObject); | ||
}); | ||
|
||
test('should throw an error if input is invalid JSON and invalid YAML', () => { | ||
try { | ||
convertToJson(invalidString); | ||
expect(convertToJson(invalidString)).toBeUndefined(); | ||
} catch (error) { | ||
expect(error.message.includes("Invalid content format")).toBeTruthy(); | ||
} | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also check that there shouldn't be any value returned from the function.