Skip to content

Commit ebf9cd8

Browse files
committed
Add basic tests in jest
1 parent 8667927 commit ebf9cd8

File tree

4 files changed

+1866
-25
lines changed

4 files changed

+1866
-25
lines changed

README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,8 @@ For ESM:
4545
```js
4646
import parser from 'bbcode-parser-sax'
4747
return parser.parse(BBCODE_STRING)
48-
```
48+
```
49+
50+
## Tests
51+
This package has a few basic tests to confirm the parser is working.
52+
Run `yarn test` to execute the tests.

package.json

+8-2
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,18 @@
88
"license": "MIT",
99
"scripts": {
1010
"build-parser": "pegjs -o bbcode-parser.js grammar.pegjs",
11-
"build": "yarn build-parser && parcel build"
11+
"build": "yarn build-parser && parcel build",
12+
"test": "jest"
1213
},
1314
"devDependencies": {
15+
"jest": "^27.4.7",
1416
"parcel": "^2.2.1",
1517
"pegjs": "^0.10.0"
1618
},
17-
"keywords": ["bbcode", "parser", "sax"],
19+
"keywords": [
20+
"bbcode",
21+
"parser",
22+
"sax"
23+
],
1824
"homepage": "https://github.com/an0nusr/bbcode-parser-sax"
1925
}

parser.test.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const { parse } = require("./bbcode-parser")
2+
3+
test("check basic text parses without tags", () => {
4+
let res = parse("I am text")
5+
expect(res.length).toBe(1)
6+
expect(res[0].type).toBe("text")
7+
expect(res[0].text).toBe("I am text")
8+
})
9+
10+
test("check basic tag functionality", () => {
11+
let res = parse("[b]bold![/b]")
12+
expect(res.length).toBe(3)
13+
expect(res[0].type).toBe("startTag")
14+
expect(res[0].tag.name).toBe("b")
15+
expect(res[1].type).toBe("text")
16+
expect(res[2].type).toBe("endTag")
17+
expect(res[2].tag.name).toBe("b")
18+
})
19+
20+
test("check tag attributes", () => {
21+
let res = parse("[url=https://site.com?q=1]")
22+
expect(res.length).toBe(1)
23+
expect(res[0].type).toBe("startTag")
24+
25+
let tag = res[0].tag
26+
expect(tag.name).toBe("url")
27+
expect(tag.value).toBe("https://site.com?q=1")
28+
})

0 commit comments

Comments
 (0)