Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Automated testing with Mocha#176

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
ImVietnam wants to merge45 commits intojavascript-tutorial:master
base:master
Choose a base branch
Loading
fromImVietnam:patch-4
Open
Show file tree
Hide file tree
Changes fromall commits
Commits
Show all changes
45 commits
Select commitHold shift + click to select a range
4a83ba8
Update article.md
ImVietnamFeb 23, 2023
f34e687
Update index.html
ImVietnamFeb 28, 2023
1cd8cac
Update task.md
ImVietnamFeb 28, 2023
d28983b
Update solution.md
ImVietnamFeb 28, 2023
d5b7318
Update test.js
ImVietnamFeb 28, 2023
c3c1286
Update index.html
ImVietnamFeb 28, 2023
4000563
Update index.html
ImVietnamFeb 28, 2023
4a9a8c2
Update index.html
ImVietnamFeb 28, 2023
6802cf9
Update index.html
ImVietnamFeb 28, 2023
b230aed
Update index.html
ImVietnamFeb 28, 2023
7843e6e
Update index.html
ImVietnamFeb 28, 2023
3017a30
Update test.js
ImVietnamFeb 28, 2023
689c105
Update index.html
ImVietnamFeb 28, 2023
fbc2fc9
Update index.html
ImVietnamFeb 28, 2023
67d6fd5
Update index.html
ImVietnamFeb 28, 2023
7a19c9d
Update article.md
ImVietnamMar 2, 2023
43edb37
Update article.md
ImVietnamMar 3, 2023
dfb90da
Update article.md
ImVietnamMar 3, 2023
1e01c55
Update article.md
ImVietnamMar 3, 2023
552b5cf
Update article.md
ImVietnamMar 3, 2023
20b697e
Update article.md
ImVietnamMar 3, 2023
7e42dc6
Merge branch 'javascript-tutorial:master' into patch-4
ImVietnamJun 7, 2023
d3c091b
Update article.md
ImVietnamJun 7, 2023
9fb89f4
Update article.md
ImVietnamJun 7, 2023
26917ea
Update article.md
ImVietnamJun 7, 2023
c37ac95
Update index.html
ImVietnamJun 7, 2023
f30225c
Update index.html
ImVietnamJun 7, 2023
1b8289f
Update test.js
ImVietnamJun 7, 2023
c66c52c
Update index.html
ImVietnamJun 7, 2023
040d2d2
Update index.html
ImVietnamJun 7, 2023
f423ac6
Update index.html
ImVietnamJun 7, 2023
bba2ab3
Update index.html
ImVietnamJun 7, 2023
f698b13
Update index.html
ImVietnamJun 7, 2023
2b2cbad
Update index.html
ImVietnamJun 7, 2023
553dfd2
Update index.html
ImVietnamJun 7, 2023
50750e9
Update solution.md
ImVietnamJun 7, 2023
38b5f2c
Update task.md
ImVietnamJun 7, 2023
8c22db5
Update index.html
ImVietnamJun 7, 2023
ac1334d
Update index.html
ImVietnamJun 7, 2023
51b6380
Update index.html
ImVietnamJun 7, 2023
2cac560
Update index.html
ImVietnamJun 7, 2023
f0c8678
Update article.md
ImVietnamJun 7, 2023
aaea552
Update article.md
ImVietnamJun 7, 2023
0a79d79
Merge branch 'javascript-tutorial:master' into patch-4
ImVietnamJun 9, 2023
e2d9de3
Merge branch 'javascript-tutorial:master' into patch-4
ImVietnamAug 2, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,49 @@
The test demonstrates one of the temptations a developer meets when writing tests.
Bài kiểm tra thể hiện một trong những cám dỗ mà nhà phát triển gặp phải khi viết bài kiểm tra.

What we have here is actually 3 tests, but layed out as a single function with 3 asserts.
Những gì chúng ta có ở đây thực sự là 3 bài kiểm tra, nhưng được bố trí dưới dạng một hàm duy nhất với 3 lần xác nhận.

Sometimes it's easier to write this way, but if an error occurs, it's much less obvious what went wrong.
Đôi khi viết theo cách này dễ dàng hơn, nhưng nếu có lỗi xảy ra, thì sẽ khó nhận ra điều gì đã xảy ra.

If an error happens in the middle of a complex execution flow, then we'll have to figure out the data at that point. We'll actually have to *debug the test*.
Nếu xảy ra lỗi ở giữa quy trình thực thi phức tạp, thì chúng ta sẽ phải tìm ra dữ liệu tại thời điểm đó. Chúng ta thực sự sẽ phải *gỡ lỗi cho bài kiểm tra*.

It would be much better to break the test into multiple`it`blocks with clearly written inputs and outputs.
Sẽ tốt hơn nhiều nếu chia bài kiểm tra thành nhiều khối`it`với đầu vào và đầu ra được viết rõ ràng.

Like this:
Như thế này:
```js
describe("Raises xto power n", function() {
it("5in the power of 1 equals 5", function() {
describe("Nâng xlên luỹ thừa n", function() {
it("5mũ 1 bằng 5", function() {
assert.equal(pow(5, 1), 5);
});

it("5in the power of 2 equals 25", function() {
it("5mũ 2 bằng 25", function() {
assert.equal(pow(5, 2), 25);
});

it("5in the power of 3 equals 125", function() {
it("5mũ 3 bằng 125", function() {
assert.equal(pow(5, 3), 125);
});
});
```

We replaced the single`it`with `describe`and a group of`it` blocks. Now if something fails we would see clearly what the data was.
Chúng ta đã thay thế một khối`it`bằng `describe`và một nhóm các khối`it`. Bây giờ nếu có gì đó không thành công, chúng ta sẽ thấy rõ ràng dữ liệu là gì.

Also we can isolate a single test and run it in standalone mode by writing`it.only`instead of `it`:
Ngoài ra, chúng ta có thể tách riêng một bài kiểm tra và chạy nó ở chế độ độc lập bằng cách viết`it.only`thay vì `it`:


```js
describe("Raises xto power n", function() {
it("5in the power of 1 equals 5", function() {
describe("Nâng xlên luỹ thừa n", function() {
it("5mũ 1 bằng 5", function() {
assert.equal(pow(5, 1), 5);
});

*!*
// Mochawill run only thisblock
it.only("5in the power of 2 equals 25", function() {
// Mochasẽ chỉ chạyblock này
it.only("5mũ 2 bằng 25", function() {
assert.equal(pow(5, 2), 25);
});
*/!*

it("5in the power of 3 equals 125", function() {
it("5mũ 3 bằng 125", function() {
assert.equal(pow(5, 3), 125);
});
});
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,12 +2,12 @@ importance: 5

---

#What's wrong in the test?
#Có gì sai trong bài kiểm tra?

What's wrong in the test of`pow`below?
Có gì sai trong bài kiểm tra`pow`bên dưới?

```js
it("Raises xto the power n", function() {
it("Nâng xlên luỹ thừa n", function() {
let x = 5;

let result = x;
Expand All@@ -21,4 +21,4 @@ it("Raises x to the power n", function() {
});
```

P.S. Syntactically the test is correct and passes.
Tái bút: Về mặt cú pháp, bài kiểm tra là chính xác và đỗ.
310 changes: 155 additions & 155 deletions1-js/03-code-quality/05-testing-mocha/article.md
View file
Open in desktop

Large diffs are not rendered by default.

View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
<!DOCTYPE html>
<html>
<head>
<!--add mocha css,to show results -->
<!--thêm mocha css,để hiển thị kết quả -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/mocha/3.2.0/mocha.css">
<!--addmocha framework code -->
<!--thêm mãmocha framework -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/mocha/3.2.0/mocha.js"></script>
<script>
//enable bdd-style testing
//bật kiểm tra kiểu bdd
mocha.setup('bdd');
</script>
<!--add chai -->
<!--thêm chai -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/chai/3.5.0/chai.js"></script>
<script>
// chaihas a lot of stuff, let's make assert global
// chaicó rất nhiều thứ, hãy xác nhận chung
let assert = chai.assert;
</script>
</head>

<body>

<!--the script with tests (describe, it...) -->
<!--tập lệnh với các bài kiểm tra (describe, it...) -->
<script src="test.js"></script>

<!--the element with id="mocha"will contain test results -->
<!--phần tử có id="mocha"sẽ chứa kết quả kiểm tra -->
<div id="mocha"></div>

<!--run tests! -->
<!--chạy các bài kiểm tra! -->
<script>
mocha.run();
</script>
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
describe("test", function() {

// Mochausually waits for the tests for 2 seconds before considering them wrong
// Mochathường đợi các bài kiểm tra trong 2 giây trước khi coi chúng sai

this.timeout(200000); //With this code we increase this - in this case to200,000milliseconds
this.timeout(200000); //Với mã này, chúng ta tăng cái này (timeout) - trong trường hợp này là200.000mili giây

//This is because of the "alert" function, because if you delay pressing the"OK"button the tests will not pass!
//Điều này là do hàm "alert", bởi vì nếu bạn trì hoãn nhấn nút"OK"thì các bài kiểm tra sẽ không vượt qua!

before(() => alert("Testing started – before all tests"));
after(() => alert("Testing finished – after all tests"));
before(() => alert("Bài kiểm tra bắt đầu – trước tất cả các bài kiểm tra"));
after(() => alert("Bài kiểm tra kết thúc – sau tất cả các bài kiểm tra"));

beforeEach(() => alert("Before a test – enter a test"));
afterEach(() => alert("After a test – exit a test"));
beforeEach(() => alert("Trước một bài kiểm tra – nhập một bài kiểm tra"));
afterEach(() => alert("Trước một bài kiểm tra – nhập một bài kiểm tra"));

it('test 1', () => alert(1));
it('test 2', () => alert(2));
Expand Down
18 changes: 9 additions & 9 deletions1-js/03-code-quality/05-testing-mocha/index.html
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
<!DOCTYPE html>
<html>
<head>
<!--add mocha css,to show results -->
<!--thêm mocha css,để hiển thị kết quả -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/mocha/3.2.0/mocha.css">
<!--addmocha framework code -->
<!--thêm mãmocha framework -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/mocha/3.2.0/mocha.js"></script>
<script>
mocha.setup('bdd'); //minimal setup
mocha.setup('bdd'); //thiết lập tối thiểu
</script>
<!--add chai -->
<!--thêm chai -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/chai/3.5.0/chai.js"></script>
<script>
// chaihas a lot of stuff, let's make assert global
// chaicó rất nhiều thứ, hãy xác nhận chung
let assert = chai.assert;
</script>
</head>
Expand All@@ -20,17 +20,17 @@

<script>
function pow(x, n) {
/*function code is to be written, empty now */
/*mã hàm sẽ được viết, bây giờ đang trống */
}
</script>

<!--the script with tests (describe, it...) -->
<!--tập lệnh với các bài kiểm tra (describe, it...) -->
<script src="test.js"></script>

<!--the element with id="mocha"will contain test results -->
<!--phần tử có id="mocha"sẽ chứa kết quả kiểm tra -->
<div id="mocha"></div>

<!--run tests! -->
<!--chạy các bài kiểm tra! -->
<script>
mocha.run();
</script>
Expand Down
18 changes: 9 additions & 9 deletions1-js/03-code-quality/05-testing-mocha/pow-1.view/index.html
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
<!DOCTYPE html>
<html>
<head>
<!--add mocha css,to show results -->
<!--thêm mocha css,để hiển thị kết quả -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/mocha/3.2.0/mocha.css">
<!--addmocha framework code -->
<!--thêm mãmocha framework -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/mocha/3.2.0/mocha.js"></script>
<script>
//enable bdd-style testing
//bật kiểm tra kiểu bdd
mocha.setup('bdd');
</script>
<!--add chai -->
<!--thêm chai -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/chai/3.5.0/chai.js"></script>
<script>
// chaihas a lot of stuff, let's make assert global
// chaicó rất nhiều thứ, hãy xác nhận chung
let assert = chai.assert;
</script>
</head>
Expand All@@ -21,17 +21,17 @@

<script>
function pow(x, n) {
/*function code is to be written, empty now */
/*mã hàm sẽ được viết, bây giờ đang trống */
}
</script>

<!--the script with tests (describe, it...) -->
<!--tập lệnh với các bài kiểm tra (describe, it...) -->
<script src="test.js"></script>

<!--the element with id="mocha"will contain test results -->
<!--phần tử có id="mocha"sẽ chứa kết quả kiểm tra -->
<div id="mocha"></div>

<!--run tests! -->
<!--chạy các bài kiểm tra! -->
<script>
mocha.run();
</script>
Expand Down
18 changes: 9 additions & 9 deletions1-js/03-code-quality/05-testing-mocha/pow-2.view/index.html
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
<!DOCTYPE html>
<html>
<head>
<!--add mocha css,to show results -->
<!--thêm mocha css,để hiển thị kết quả -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/mocha/3.2.0/mocha.css">
<!--addmocha framework code -->
<!--thêm mãmocha framework -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/mocha/3.2.0/mocha.js"></script>
<script>
//enable bdd-style testing
//bật kiểm tra kiểu bdd
mocha.setup('bdd');
</script>
<!--add chai -->
<!--thêm chai -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/chai/3.5.0/chai.js"></script>
<script>
// chaihas a lot of stuff, let's make assert global
// chaicó rất nhiều thứ, hãy xác nhận chung
let assert = chai.assert;
</script>
</head>
Expand All@@ -21,17 +21,17 @@

<script>
function pow(x, n) {
return 8; // :)we cheat!
return 8; // :)chúng ta gian lận!
}
</script>

<!--the script with tests (describe, it...) -->
<!--tập lệnh với các bài kiểm tra (describe, it...) -->
<script src="test.js"></script>

<!--the element with id="mocha"will contain test results -->
<!--phần tử có id="mocha"sẽ chứa kết quả kiểm tra -->
<div id="mocha"></div>

<!--run tests! -->
<!--chạy các bài kiểm tra! -->
<script>
mocha.run();
</script>
Expand Down
16 changes: 8 additions & 8 deletions1-js/03-code-quality/05-testing-mocha/pow-3.view/index.html
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
<!DOCTYPE html>
<html>
<head>
<!--add mocha css,to show results -->
<!--thêm mocha css,để hiển thị kết quả -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/mocha/3.2.0/mocha.css">
<!--addmocha framework code -->
<!--thêm mãmocha framework -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/mocha/3.2.0/mocha.js"></script>
<script>
//enable bdd-style testing
//bật kiểm tra kiểu bdd
mocha.setup('bdd');
</script>
<!--add chai -->
<!--thêm chai -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/chai/3.5.0/chai.js"></script>
<script>
// chaihas a lot of stuff, let's make assert global
// chaicó rất nhiều thứ, hãy xác nhận chung
let assert = chai.assert;
</script>
</head>
Expand All@@ -31,13 +31,13 @@
}
</script>

<!--the script with tests (describe, it...) -->
<!--tập lệnh với các bài kiểm tra (describe, it...) -->
<script src="test.js"></script>

<!--the element with id="mocha"will contain test results -->
<!--phần tử có id="mocha"sẽ chứa kết quả kiểm tra -->
<div id="mocha"></div>

<!--run tests! -->
<!--chạy các bài kiểm tra! -->
<script>
mocha.run();
</script>
Expand Down
16 changes: 8 additions & 8 deletions1-js/03-code-quality/05-testing-mocha/pow-4.view/index.html
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
<!DOCTYPE html>
<html>
<head>
<!--add mocha css,to show results -->
<!--thêm mocha css,để hiển thị kết quả -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/mocha/3.2.0/mocha.css">
<!--addmocha framework code -->
<!--thêm mãmocha framework -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/mocha/3.2.0/mocha.js"></script>
<script>
//enable bdd-style testing
//bật kiểm tra kiểu bdd
mocha.setup('bdd');
</script>
<!--add chai -->
<!--thêm chai -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/chai/3.5.0/chai.js"></script>
<script>
// chaihas a lot of stuff, let's make assert global
// chaicó rất nhiều thứ, hãy xác nhận chung
let assert = chai.assert;
</script>
</head>
Expand All@@ -31,13 +31,13 @@
}
</script>

<!--the script with tests (describe, it...) -->
<!--tập lệnh với các bài kiểm tra (describe, it...) -->
<script src="test.js"></script>

<!--the element with id="mocha"will contain test results -->
<!--phần tử có id="mocha"sẽ chứa kết quả kiểm tra -->
<div id="mocha"></div>

<!--run tests! -->
<!--chạy các bài kiểm tra! -->
<script>
mocha.run();
</script>
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -15,5 +15,5 @@ describe("pow", function() {

});

// ...more tests to follow here, both describeand itcan be added
// ...nhiều bài kiểm tra để theo dõi ở đây, cả describe itcó thể được thêm vào
});
Loading

[8]ページ先頭

©2009-2025 Movatter.jp