-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from narol1024/develop
fix: clean unit testing code
- Loading branch information
Showing
8 changed files
with
117 additions
and
162 deletions.
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
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,53 @@ | ||
/** | ||
* This is a sample test suite. | ||
* Replace this with your implementation. | ||
*/ | ||
import { exec } from 'child_process'; | ||
import fetch from 'node-fetch'; | ||
|
||
jest.mock('child_process'); | ||
jest.mock('node-fetch'); | ||
|
||
const dependantsHtml = ` | ||
<html> | ||
<body> | ||
<a id="package-tab-dependents" tabindex="0"><span><svg></svg>10000 Dependents</span></a> | ||
</body> | ||
</html> | ||
`; | ||
|
||
(exec as any).mockImplementation((command: any, callback: any) => { | ||
if (command.includes('is-a-non-existent-package')) { | ||
callback(null, { stdout: '{"error": { "code": "E404" }}' }); | ||
} else if (/react@\d+\.\d+\.\d+/.test(command)) { | ||
callback(null, { | ||
stdout: `{ "fbjs": "^0.8.16", "prop-types": "^15.6.0", "loose-envify": "^1.1.0", "object-assign": "^4.1.1" }`, | ||
}); | ||
} else if (/vue@\d+\.\d+\.\d+/.test(command)) { | ||
callback(null, { | ||
stdout: `{ "@vue/shared": "3.0.0", "@vue/compiler-dom": "3.0.0", "@vue/runtime-dom": "3.0.0" }`, | ||
}); | ||
} else if (/\sreact\s/.test(command)) { | ||
callback(null, { stdout: '{ "loose-envify": "^1.1.0" }' }); | ||
} else if (/\svue\s/.test(command)) { | ||
callback(null, { | ||
stdout: `{ "@vue/shared": "3.4.27", "@vue/compiler-dom": "3.4.27", "@vue/compiler-sfc": "3.4.27", "@vue/runtime-dom": "3.4.27", "@vue/server-renderer": "3.4.27" }`, | ||
}); | ||
} else { | ||
callback(null, { stdout: '{}' }); | ||
} | ||
}); | ||
|
||
(fetch as any).mockImplementation((args: any) => { | ||
// It returns 404 status when fetching a invalid package. | ||
if (args.includes('is-a-non-existent-package')) { | ||
return Promise.resolve({ | ||
status: 404, | ||
text: () => '', | ||
}); | ||
} | ||
return Promise.resolve({ | ||
status: 200, | ||
text: () => dependantsHtml, | ||
}); | ||
}); |
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
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 |
---|---|---|
|
@@ -2,111 +2,45 @@ | |
* This is a sample test suite. | ||
* Replace this with your implementation. | ||
*/ | ||
import fetch from 'node-fetch'; | ||
import { exec } from 'child_process'; | ||
import { readNpmPackageDependencies, countNpmPackageDependants, analyze } from './index'; | ||
|
||
const dependantsHtml = ` | ||
<html> | ||
<body> | ||
<a id="package-tab-dependents" tabindex="0"><span><svg></svg>10000 Dependents</span></a> | ||
</body> | ||
</html> | ||
`; | ||
|
||
jest.mock('child_process'); | ||
jest.mock('node-fetch'); | ||
|
||
// TODO: | ||
// describe('Npm pacakge Tests for the real world', () => { | ||
// it('can be analyzed for single package', async () => { | ||
// await expect(analyze('react')).resolves.toMatchSnapshot(); | ||
// }); | ||
// }); | ||
|
||
describe('Npm pacakge Tests', () => { | ||
describe('Npm package tests', () => { | ||
it('should not read a non-existent package', async () => { | ||
(exec as any).mockImplementationOnce((_: any, callback: any) => { | ||
callback(null, { stdout: '{"error": { "code": "E404" }}' }); | ||
}); | ||
await expect(readNpmPackageDependencies('is-a-non-existent-package')).rejects.toEqual( | ||
new Error('Cannot read is-a-non-existent-package.'), | ||
); | ||
}); | ||
it('should read a valid package', async () => { | ||
(exec as any).mockImplementationOnce((_: any, callback: any) => { | ||
callback(null, { stdout: '{ "loose-envify": "^1.1.0" }' }); | ||
}); | ||
await expect(readNpmPackageDependencies('[email protected]')).resolves.toEqual([ | ||
await expect(readNpmPackageDependencies('[email protected]')).resolves.toEqual([ | ||
{ name: 'fbjs', version: '^0.8.16' }, | ||
{ name: 'prop-types', version: '^15.6.0' }, | ||
{ name: 'loose-envify', version: '^1.1.0' }, | ||
{ name: 'object-assign', version: '^4.1.1' }, | ||
]); | ||
}); | ||
it('should count the depandants of a valid package', async () => { | ||
(fetch as any).mockReturnValueOnce( | ||
Promise.resolve({ | ||
status: 200, | ||
text: () => dependantsHtml, | ||
}), | ||
); | ||
await expect(countNpmPackageDependants('react', '18.3.1')).resolves.toEqual(10000); | ||
await expect(countNpmPackageDependants('react', '16.0.0')).resolves.toEqual(10000); | ||
}); | ||
it('can not count the depandants of a invalid package', async () => { | ||
(fetch as any).mockReturnValueOnce( | ||
Promise.resolve({ | ||
status: 200, | ||
text: () => '', | ||
}), | ||
); | ||
await expect(countNpmPackageDependants('is-a-non-existent-package', '1.0.0')).rejects.toEqual( | ||
new Error(`Cannot count the depandants of the is-a-non-existent-package.`), | ||
); | ||
}); | ||
it('can be analyzed for single package', async () => { | ||
(exec as any).mockImplementationOnce((_: any, callback: any) => { | ||
callback(null, { stdout: '{ "loose-envify": "^1.1.0" }' }); | ||
}); | ||
(fetch as any).mockReturnValueOnce( | ||
Promise.resolve({ | ||
status: 200, | ||
text: () => dependantsHtml, | ||
}), | ||
it('can be analyzed for a invalid package', async () => { | ||
await expect(analyze('is-a-non-existent-package')).rejects.toEqual( | ||
new Error(`Cannot analyze is-a-non-existent-package, the reason is "Cannot read is-a-non-existent-package."`), | ||
); | ||
}); | ||
it('can be analyzed for single package', async () => { | ||
await expect(analyze('react')).resolves.toMatchSnapshot(); | ||
}); | ||
it('can be analyzed for multiple packages', async () => { | ||
(exec as any).mockImplementation((command: string, callback: any) => { | ||
if (command.includes('react')) { | ||
callback(null, { stdout: '{ "loose-envify": "^1.1.0" }' }); | ||
} else if (command.includes('vue')) { | ||
callback(null, { | ||
stdout: `{ "@vue/shared": "3.4.27", "@vue/compiler-dom": "3.4.27", "@vue/compiler-sfc": "3.4.27", "@vue/runtime-dom": "3.4.27", "@vue/server-renderer": "3.4.27" }`, | ||
}); | ||
} else { | ||
callback(null, { stdout: '{}' }); | ||
} | ||
}); | ||
(fetch as any).mockReturnValue( | ||
Promise.resolve({ | ||
status: 200, | ||
text: () => dependantsHtml, | ||
}), | ||
); | ||
await expect(analyze('react,vue')).resolves.toMatchSnapshot(); | ||
}); | ||
it('can be analyzed for a zero dependency library', async () => { | ||
await expect(analyze('[email protected]')).resolves.toMatchSnapshot(); | ||
}); | ||
it('can be analyzed for multiple packages with the package version', async () => { | ||
(exec as any).mockImplementation((command: string, callback: any) => { | ||
if (command.includes('react')) { | ||
callback(null, { | ||
stdout: `{ "fbjs": "^0.8.16", "prop-types": "^15.6.0", "loose-envify": "^1.1.0", "object-assign": "^4.1.1" }`, | ||
}); | ||
} else if (command.includes('vue')) { | ||
callback(null, { | ||
stdout: `{ "@vue/shared": "3.0.0", "@vue/compiler-dom": "3.0.0", "@vue/runtime-dom": "3.0.0" }`, | ||
}); | ||
} else { | ||
callback(null, { stdout: '{}' }); | ||
} | ||
}); | ||
await expect(analyze('[email protected],[email protected]')).resolves.toMatchSnapshot(); | ||
}); | ||
}); |
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