Skip to content

Commit

Permalink
add test case for Optional Chaining
Browse files Browse the repository at this point in the history
  • Loading branch information
cenfun committed Aug 15, 2024
1 parent 45efb7e commit c2beae9
Show file tree
Hide file tree
Showing 17 changed files with 293 additions and 132 deletions.
41 changes: 25 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions test/mock/src/branch/branch.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const IfStatement = require('./if.js');
const LogicalExpression = require('./logical.js');
const SwitchStatement = require('./switch.js');
const SwitchStatementNoBreak = require('./switch-no-break.js');
const OptionalChaining = require('./optional-chaining.js');

const uncoveredFunction = (a) => {
const list = [1, 2, 3, 4, 5];
Expand Down Expand Up @@ -154,6 +155,7 @@ const branch = (a) => {
LogicalExpression();
SwitchStatement();
SwitchStatementNoBreak();
OptionalChaining();
};

module.exports = {
Expand Down
34 changes: 34 additions & 0 deletions test/mock/src/branch/optional-chaining.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Currently, The `Optional chaining` will be counted as:
* ✅ bytes coverage
* ✅ lines coverage
* ❌ not branches coverage
*/

const adventurer = {
name: 'Alice',
cat: {
name: 'Dinah'
}
};

// obj.val?.prop
// obj.val?.[expr]
// obj.func?.(args)

function OptionalChaining() {

console.log(adventurer.cat?.name);

// Expected output: undefined
console.log(adventurer.dog?.name);

// Expected output: undefined
console.log(adventurer.someNonExistentMethod?.());

console.log(adventurer.cat?.first?.second);
}

module.exports = () => {
OptionalChaining();
};
2 changes: 1 addition & 1 deletion test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"mocha": "^10.7.3",
"open": "^10.1.0",
"playwright": "^1.46.0",
"puppeteer": "^23.0.2",
"puppeteer": "^23.1.0",
"rollup": "^4.20.0",
"swc-loader": "^0.2.6",
"ts-loader": "^9.5.1",
Expand Down
27 changes: 18 additions & 9 deletions test/snapshot/cli.snapshot.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"type": "v8",
"summary": {
"bytes": "53.59 %",
"statements": "56.61 %",
"bytes": "54.97 %",
"statements": "57.37 %",
"branches": "52.42 %",
"functions": "50.54 %",
"lines": "45.58 %"
"functions": "51.58 %",
"lines": "46.29 %"
},
"files": {
"test/mock/node/lib/app.js": {
Expand Down Expand Up @@ -65,11 +65,11 @@
"test/mock/src/branch/branch.js": {
"functions": "70.00 %",
"branches": "65.85 %",
"statements": "85.14 %",
"lines": "74.34 %",
"bytes": "81.49 %",
"uncoveredLines": "10-20,28,57-60,74,81-83,108,119-124,136-139,144-146",
"extras": "1c,2b,9b,15b,17c,21b,26b,27c,30b,31c,35b,36c,37c,38c,39c,43b,45b,47b,56b,58c,61b,70b,72b,73c,76b,77c,85b,87b,98b,100b,105b,107c,111b,113b,115b,117b,118b,122c,125b,127b,134b,135b,137c,140b,141c,142c,150b,158b,162b"
"statements": "85.53 %",
"lines": "74.78 %",
"bytes": "81.98 %",
"uncoveredLines": "11-21,29,58-61,75,82-84,109,120-125,137-140,145-147",
"extras": "1c,2b,10b,16b,18c,22b,27b,28c,31b,32c,36b,37c,38c,39c,40c,44b,46b,48b,57b,59c,62b,71b,73b,74c,77b,78c,86b,88b,99b,101b,106b,108c,112b,114b,116b,118b,119b,123c,126b,128b,135b,136b,138c,141b,142c,143c,151b,160b,164b"
},
"test/mock/src/branch/conditional.js": {
"functions": "100.00 %",
Expand Down Expand Up @@ -98,6 +98,15 @@
"uncoveredLines": "22-23,33-34,38,45,48-49,59-60",
"extras": "1c,2b,4b,10b,15b,19b,25b,29b,36b,40b,47b,51b,58b,62b,65b,66b,68b,73b,75b"
},
"test/mock/src/branch/optional-chaining.js": {
"functions": "100.00 %",
"branches": "",
"statements": "100.00 %",
"lines": "80.00 %",
"bytes": "97.21 %",
"uncoveredLines": "24-29",
"extras": "1c,2c,3c,4c,5c,6c,7b,14b,15c,16c,17c,18b,20b,22b,23c,25b,26c,28b,31b,35b"
},
"test/mock/src/branch/switch-no-break.js": {
"functions": "100.00 %",
"branches": "100.00 %",
Expand Down
27 changes: 18 additions & 9 deletions test/snapshot/esbuild.snapshot.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"type": "v8",
"summary": {
"bytes": "82.76 %",
"statements": "83.73 %",
"bytes": "83.28 %",
"statements": "84.05 %",
"branches": "60.08 %",
"functions": "84.16 %",
"lines": "72.48 %"
"functions": "84.62 %",
"lines": "72.70 %"
},
"files": {
"src/async.js": {
Expand All @@ -29,11 +29,11 @@
"src/branch/branch.js": {
"functions": "72.73 %",
"branches": "65.85 %",
"statements": "86.08 %",
"lines": "74.34 %",
"bytes": "81.46 %",
"uncoveredLines": "10-20,28,57-60,74,81-83,108,119-124,136-139,144-146",
"extras": "1c,2b,9b,15b,17c,21b,26b,27c,30b,31c,35b,36c,37c,38c,39c,43b,45b,47b,56b,58c,61b,70b,72b,73c,76b,77c,85b,87b,98b,100b,105b,107c,111b,113b,115b,117b,118b,122c,125b,127b,134b,135b,137c,140b,141c,142c,150b,158b,162b"
"statements": "86.42 %",
"lines": "74.78 %",
"bytes": "81.95 %",
"uncoveredLines": "11-21,29,58-61,75,82-84,109,120-125,137-140,145-147",
"extras": "1c,2b,10b,16b,18c,22b,27b,28c,31b,32c,36b,37c,38c,39c,40c,44b,46b,48b,57b,59c,62b,71b,73b,74c,77b,78c,86b,88b,99b,101b,106b,108c,112b,114b,116b,118b,119b,123c,126b,128b,135b,136b,138c,141b,142c,143c,151b,160b,164b"
},
"src/branch/conditional.js": {
"functions": "100.00 %",
Expand Down Expand Up @@ -62,6 +62,15 @@
"uncoveredLines": "22-23,33-34,38,45,48-49,59-60",
"extras": "1c,2b,4b,10b,15b,19b,25b,29b,36b,40b,47b,51b,58b,62b,65b,66b,68b,73b,75b"
},
"src/branch/optional-chaining.js": {
"functions": "100.00 %",
"branches": "",
"statements": "100.00 %",
"lines": "80.00 %",
"bytes": "97.21 %",
"uncoveredLines": "24-29",
"extras": "1c,2c,3c,4c,5c,6c,7b,14b,15c,16c,17c,18b,20b,22b,23c,25b,26c,28b,31b,35b"
},
"src/branch/switch-no-break.js": {
"functions": "100.00 %",
"branches": "100.00 %",
Expand Down
20 changes: 14 additions & 6 deletions test/snapshot/istanbul.snapshot.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"type": "istanbul",
"summary": {
"lines": "82.54 %",
"statements": "82.46 %",
"functions": "77.38 %",
"lines": "82.85 %",
"statements": "82.76 %",
"functions": "77.90 %",
"branches": "58.80 %"
},
"files": {
Expand All @@ -24,11 +24,11 @@
"extras": ""
},
"branch/branch.js": {
"lines": "86.48 %",
"lines": "86.84 %",
"functions": "70.00 %",
"statements": "86.84 %",
"statements": "87.17 %",
"branches": "65.85 %",
"uncoveredLines": "11-18,59,82,120-121,138",
"uncoveredLines": "12-19,60,83,121-122,139",
"extras": ""
},
"branch/conditional.js": {
Expand All @@ -55,6 +55,14 @@
"uncoveredLines": "",
"extras": ""
},
"branch/optional-chaining.js": {
"lines": "100.00 %",
"functions": "100.00 %",
"statements": "100.00 %",
"branches": "100.00 %",
"uncoveredLines": "",
"extras": ""
},
"branch/switch-no-break.js": {
"lines": "100.00 %",
"functions": "100.00 %",
Expand Down
27 changes: 18 additions & 9 deletions test/snapshot/merge.snapshot.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"type": "v8",
"summary": {
"bytes": "75.97 %",
"statements": "78.39 %",
"bytes": "76.57 %",
"statements": "78.73 %",
"branches": "60.13 %",
"functions": "72.90 %",
"lines": "65.58 %"
"functions": "73.39 %",
"lines": "65.87 %"
},
"files": {
"my-vm-filename.js": {
Expand Down Expand Up @@ -83,11 +83,11 @@
"test/mock/src/branch/branch.js": {
"functions": "70.00 %",
"branches": "65.85 %",
"statements": "85.14 %",
"lines": "74.34 %",
"bytes": "81.49 %",
"uncoveredLines": "10-20,28,57-60,74,81-83,108,119-124,136-139,144-146",
"extras": "1c,2b,9b,15b,17c,21b,26b,27c,30b,31c,35b,36c,37c,38c,39c,43b,45b,47b,56b,58c,61b,70b,72b,73c,76b,77c,85b,87b,98b,100b,105b,107c,111b,113b,115b,117b,118b,122c,125b,127b,134b,135b,137c,140b,141c,142c,150b,158b,162b"
"statements": "85.53 %",
"lines": "74.78 %",
"bytes": "81.98 %",
"uncoveredLines": "11-21,29,58-61,75,82-84,109,120-125,137-140,145-147",
"extras": "1c,2b,10b,16b,18c,22b,27b,28c,31b,32c,36b,37c,38c,39c,40c,44b,46b,48b,57b,59c,62b,71b,73b,74c,77b,78c,86b,88b,99b,101b,106b,108c,112b,114b,116b,118b,119b,123c,126b,128b,135b,136b,138c,141b,142c,143c,151b,160b,164b"
},
"test/mock/src/branch/conditional.js": {
"functions": "100.00 %",
Expand Down Expand Up @@ -116,6 +116,15 @@
"uncoveredLines": "22-23,33-34,38,45,48-49,59-60",
"extras": "1c,2b,4b,10b,15b,19b,25b,29b,36b,40b,47b,51b,58b,62b,65b,66b,68b,73b,75b"
},
"test/mock/src/branch/optional-chaining.js": {
"functions": "100.00 %",
"branches": "",
"statements": "100.00 %",
"lines": "80.00 %",
"bytes": "97.21 %",
"uncoveredLines": "24-29",
"extras": "1c,2c,3c,4c,5c,6c,7b,14b,15c,16c,17c,18b,20b,22b,23c,25b,26c,28b,31b,35b"
},
"test/mock/src/branch/switch-no-break.js": {
"functions": "100.00 %",
"branches": "100.00 %",
Expand Down
Loading

0 comments on commit c2beae9

Please sign in to comment.