-
-
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
Conversation
✅ Deploy Preview for asyncapi-website ready!Built without sensitive environment variables
To edit notification comments on pull requests, go to your Netlify site configuration. |
⚡️ Lighthouse report for the changes in this PR:
Lighthouse ran on https://deploy-preview-3012--asyncapi-website.netlify.app/ |
scripts/write-json.js
Outdated
|
||
await writeFile(writePAth,JSON.stringify(jsonContent)); | ||
}catch(error){ | ||
console.log(error) |
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.
Use console.error
for error logging. Update the related tests.
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.
done
tests/utils.test.js
Outdated
|
||
describe('convertToJson', () => { | ||
test('should return JSON object if input is valid JSON string', () => { | ||
const jsonString = '{"name": "AsyncAPI", "age": 5}'; |
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.
Can you create an actual json file (for tests only), and read its content on the go rather than hard-coding JSON strings like this?
If fs
module isn't working in jest, you can even create js objects and use json.stringify()
to convert objects to JSON String.
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.
you can create fixtures
to store the static data that are needed in tests.
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.
done
tests/utils.test.js
Outdated
}); | ||
|
||
test('should return JavaScript object if input is valid YAML string', () => { | ||
const yamlString = 'name: AsyncAPI\nage: 5'; |
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.
Try to read actual yaml
file here, and iff its not viable, use multiline string. Example:
const yamlString = `
name: AsyncAPI
age: 5
`
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.
done
tests/utils.test.js
Outdated
|
||
test('should return null if input is invalid JSON and invalid YAML', () => { | ||
const invalidString = 'name: AsyncAPI, age: five'; | ||
const result = convertToJson(invalidString); |
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.
Enclose it in a try catch statement, and add an expect statement for verifying the error.
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.
done
tests/write-json.test.js
Outdated
test('should read a file, convert it to JSON, and write the JSON to another file', async () => { | ||
const readPath = 'config/testInput.yaml'; | ||
const writePath = 'config/testOutput.json'; | ||
const fileContent = 'name: AsyncAPI\nage: 5'; |
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.
Use multiline strings for better readability.
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.
done
tests/write-json.test.js
Outdated
test('should log an error if reading the file fails', async () => { | ||
const readPath = 'config/testInput.yaml'; | ||
const writePath = 'config/testOutput.json'; | ||
const error = new Error('File read error'); | ||
|
||
fs.readFile.mockRejectedValue(error); | ||
console.log = jest.fn(); | ||
|
||
await writeJSON(readPath, writePath); | ||
|
||
expect(console.log).toHaveBeenCalledWith(error); | ||
}); |
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.
Try using try catch block to catch error instead of mocking console.log
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.
done
scripts/write-json.js
Outdated
try{ | ||
const readContent = await readFile(readPath,'utf-8'); | ||
const jsonContent = convertToJson(readContent); | ||
|
||
await writeFile(writePAth,JSON.stringify(jsonContent)); | ||
}catch(error){ | ||
console.log(error) |
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.
Instead of catching the error for all three operations, you can use nested try catch or different try catch scopes to better write tests that would allow you know which out of these three operations (readFile
, convertToJson
, writeFile
) has failed.
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.
done
In the coverage report, there need to be other scripts as well, so that we can track which script has left, i.e., has 0 coverage. |
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.
I see that you are using console.log
statements inside test to assert the values. Instead, you can refactor the functions in such a way that they return some response based on execution, and you can then test their functionalities.
scripts/write-json.js
Outdated
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.
Make a util
directory inside scripts and add these functions inside this folder. Also change the filename to a relevant one.
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.
done
tests/utils.test.js
Outdated
|
||
describe('convertToJson', () => { | ||
test('should return JSON object if input is valid JSON string', () => { | ||
const jsonString = '{"name": "AsyncAPI", "age": 5}'; |
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.
you can create fixtures
to store the static data that are needed in tests.
@vishvamsinh28 Any updates on this PR? |
@anshgoyalevil updated the tests |
tests/utils.test.js
Outdated
try { | ||
expect(convertToJson(jsonString)).toEqual(jsonObject); | ||
} catch (err) { | ||
console.error(err); |
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.
add throw statements here also
@vishvamsinh28 I have updated the And, for test cases which are not supposed to fail, they shouldn't ideally be enclosed in a try catch block since the function being tested upon there is already enclosed in a try catch block, which is sufficient to throw an error and fail that specific test case. For example, In test('should return JSON object if input is valid JSON string', () => {
expect(convertToJson(jsonString)).toEqual(jsonObject);
}); The Update other tests whereever applicable, and make sure there is a Another thing is the prettier. Don't know why |
scripts/utils/write-json.js
Outdated
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.
Can you have a different filename here as per camelCase naming convention and a more meaningful one?
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.
yes will update it
tests/adopters/index.test.js
Outdated
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); |
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.
Is it required here?
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.
not really but good to have if we add more tests to it
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.
If it will be needed, we'll add it in the future. For now, you can remove it.
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.
Okay 👍
tests/write-json.test.js
Outdated
const error = new Error('File read error'); | ||
|
||
fs.readFile.mockRejectedValue(error); | ||
const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => {}); |
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.
Why do we need to mockImplementation
for console.error function?
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.
to prevent the output to log in the console during tests. If we log errors to the console that can clutter the output and make it harder to identify test failures when we have multiple tests
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.
I don't think there will be any problem with console error messages. You shouldn't mock this implementation as it will help us to detect the flow of the function, on a desired test.
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.
Okay 👍 I'll update it
tests/write-json.test.js
Outdated
`Error reading file from path ${readPath}:`, | ||
error | ||
); | ||
consoleErrorSpy.mockRestore(); |
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.
Do we need this as we already cleared the mocks in beforeEach
callback?
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.
yes we already have it in beforeEach and it works well even if i remove it but clearAllMocks does not restore it to its original implementations so i added it
|
||
test('should throw an error if input is invalid JSON and invalid YAML', () => { | ||
try { | ||
convertToJson(invalidString); |
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.
i just removed prettier when we added it back to the project because it was giving me a lot of lint/prettier errors 😅 |
@vishvamsinh28 Kindly update PR title, and resolve merge conflicts |
Merging since it already got Akshat's approval 🚢 |
/rtm |
This PR sets up Jest for testing Node.js scripts, refactors few existing scripts for better readability and maintainability, and adds initial tests to ensure functionality. It also includes coverage reporting setup and updates .gitignore to exclude coverage files.