UNPKG

5.56 kBPlain TextView Raw
1import { fixture } from './test-functions'
2import JavascriptParser from '../src/JavascriptParser'
3import { Person, SoftwarePackage } from '@stencila/schema'
4
5import MockUrlFetcher from './MockUrlFetcher'
6
7const urlFetcher = new MockUrlFetcher()
8
9// Increase timeout (in milliseconds) to allow for HTTP requests
10// to get package meta data
11jest.setTimeout(30 * 60 * 1000)
12describe('JavascriptParser', () => {
13 /**
14 * When applied to an empty folder, parse should return null.
15 */
16 test('parse:empty', async () => {
17 const parser = new JavascriptParser(urlFetcher, fixture('empty'))
18 expect(await parser.parse()).toBeNull()
19 })
20
21 /**
22 * When applied to a folder with no JS code, parse should return null.
23 */
24 test('parse:non-js', async () => {
25 const parser = new JavascriptParser(urlFetcher, fixture('r-date'))
26 expect(await parser.parse()).toBeNull()
27 })
28
29 /**
30 * When applied to a folder with a `package.json` file, parse should return
31 * a `SoftwarePackage` with `name`, `softwareRequirements` etc
32 * populated correctly.
33 */
34 test('parse:js-requirements', async () => {
35 const parser = new JavascriptParser(urlFetcher, fixture('js-requirements'))
36 const pkg = await parser.parse() as SoftwarePackage
37
38 expect(pkg.name).toEqual('js-package')
39 expect(pkg.license).toEqual('ISC')
40 expect(pkg.authors).toEqual([Person.fromText('Jason Bloggs <j.bloggs@example.com> (https://jbloggs.example.com)')])
41 expect(pkg.codeRepository).toEqual('https://github.com/stencila/dockter/')
42 expect(pkg.softwareRequirements.length).toEqual(5)
43 const expecteds = [
44 ['is-array', '1.0.1'],
45 ['mkdirp', '0.5.1'],
46 ['rimraf', '2.6.2'],
47 ['array-swap', '0.0.2'],
48 ['a-package-that-is-not-on-npm', 'org/repo']
49 ]
50
51 for (let index in expecteds) {
52 let { name, version } = pkg.softwareRequirements[index]
53 expect(name).toEqual(expecteds[index][0])
54 expect(version).toEqual(expecteds[index][1])
55 }
56 })
57
58 /**
59 * When applied to a folder with a `package.json` file with `author` as a
60 * string instead of an object, parse should return `authors` populated correctly.
61 */
62 test('parse:js-requirements', async () => {
63 const parser = new JavascriptParser(urlFetcher, fixture('js-requirements-author-string'))
64 const pkg = await parser.parse() as SoftwarePackage
65
66 expect(pkg.authors).toEqual([Person.fromText('Jason Bloggs <j.bloggs@example.com> (https://jbloggs.example.com)')])
67 })
68
69 /**
70 * When applied to a folder with a `package.json` file with `repository` as a
71 * shortcut string (https://docs.npmjs.com/files/package.json) instead of an object,
72 * parse should return `codeRepository` populated correctly.
73 */
74 test('parse:js-requirements-github-shortcut', async () => {
75 const parser = new JavascriptParser(urlFetcher, fixture('js-requirements-github-shortcut'))
76 const pkg = await parser.parse() as SoftwarePackage
77 expect(pkg.codeRepository).toEqual('https://github.com/stencila/dockter/')
78 })
79 test('parse:js-requirements-gitlab-shortcut', async () => {
80 const parser = new JavascriptParser(urlFetcher, fixture('js-requirements-gitlab-shortcut'))
81 const pkg = await parser.parse() as SoftwarePackage
82 expect(pkg.codeRepository).toEqual('https://gitlab.com/stencila/dockter/')
83 })
84 test('parse:js-requirements-bitbucket-shortcut', async () => {
85 const parser = new JavascriptParser(urlFetcher, fixture('js-requirements-bitbucket-shortcut'))
86 const pkg = await parser.parse() as SoftwarePackage
87 expect(pkg.codeRepository).toEqual('https://bitbucket.com/stencila/dockter/')
88 })
89 test('parse:js-requirements-npm-shortcut', async () => {
90 const parser = new JavascriptParser(urlFetcher, fixture('js-requirements-npm-shortcut'))
91 const pkg = await parser.parse() as SoftwarePackage
92 expect(pkg.codeRepository).toEqual('https://www.npmjs.com/package/@stencila/dockter/')
93 })
94 test('parse:js-requirements-repo-string', async () => {
95 const parser = new JavascriptParser(urlFetcher, fixture('js-requirements-repo-string'))
96 const pkg = await parser.parse() as SoftwarePackage
97 expect(pkg.codeRepository).toEqual('https://website.com/users/project/')
98 })
99
100 /**
101 * When applied to a folder with a `*.js` files, parse should return
102 * a `SoftwarePackage` with `name`, `softwareRequirements` etc
103 * populated correctly.
104 */
105 test('parse:js-sources', async () => {
106 const parser = new JavascriptParser(urlFetcher, fixture('js-sources'))
107 const pkg = await parser.parse() as SoftwarePackage
108
109 expect(pkg.name).toEqual('js-sources')
110 expect(pkg.softwareRequirements.length).toEqual(2)
111 expect(pkg.softwareRequirements[1].name).toEqual('array-swap')
112 expect(pkg.softwareRequirements[0].name).toEqual('is-sorted')
113 })
114
115 /**
116 * When applied to a folder with both `*.js` files and a `package.json` file, then parse should return a
117 * `SoftwarePackage` with `name`, `softwareRequirements` etc populated from the `package.json` in that directory.
118 */
119 test('parse:js-mixed', async () => {
120 const parser = new JavascriptParser(urlFetcher, fixture('js-mixed'))
121 const pkg = await parser.parse() as SoftwarePackage
122
123 expect(pkg.name).toEqual('js-mixed')
124 expect(pkg.description).toEqual('A test Node.js package for js-mixed')
125 expect(pkg.version).toEqual('1.2.3')
126 expect(pkg.softwareRequirements.length).toEqual(1)
127 expect(pkg.softwareRequirements[0].name).toEqual('is-even')
128 expect(pkg.softwareRequirements[0].version).toEqual('4.5.6')
129 })
130})