Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
4a83ba8
Update article.md
ImVietnam Feb 23, 2023
f34e687
Update index.html
ImVietnam Feb 28, 2023
1cd8cac
Update task.md
ImVietnam Feb 28, 2023
d28983b
Update solution.md
ImVietnam Feb 28, 2023
d5b7318
Update test.js
ImVietnam Feb 28, 2023
c3c1286
Update index.html
ImVietnam Feb 28, 2023
4000563
Update index.html
ImVietnam Feb 28, 2023
4a9a8c2
Update index.html
ImVietnam Feb 28, 2023
6802cf9
Update index.html
ImVietnam Feb 28, 2023
b230aed
Update index.html
ImVietnam Feb 28, 2023
7843e6e
Update index.html
ImVietnam Feb 28, 2023
3017a30
Update test.js
ImVietnam Feb 28, 2023
689c105
Update index.html
ImVietnam Feb 28, 2023
fbc2fc9
Update index.html
ImVietnam Feb 28, 2023
67d6fd5
Update index.html
ImVietnam Feb 28, 2023
7a19c9d
Update article.md
ImVietnam Mar 2, 2023
43edb37
Update article.md
ImVietnam Mar 3, 2023
dfb90da
Update article.md
ImVietnam Mar 3, 2023
1e01c55
Update article.md
ImVietnam Mar 3, 2023
552b5cf
Update article.md
ImVietnam Mar 3, 2023
20b697e
Update article.md
ImVietnam Mar 3, 2023
7e42dc6
Merge branch 'javascript-tutorial:master' into patch-4
ImVietnam Jun 7, 2023
d3c091b
Update article.md
ImVietnam Jun 7, 2023
9fb89f4
Update article.md
ImVietnam Jun 7, 2023
26917ea
Update article.md
ImVietnam Jun 7, 2023
c37ac95
Update index.html
ImVietnam Jun 7, 2023
f30225c
Update index.html
ImVietnam Jun 7, 2023
1b8289f
Update test.js
ImVietnam Jun 7, 2023
c66c52c
Update index.html
ImVietnam Jun 7, 2023
040d2d2
Update index.html
ImVietnam Jun 7, 2023
f423ac6
Update index.html
ImVietnam Jun 7, 2023
bba2ab3
Update index.html
ImVietnam Jun 7, 2023
f698b13
Update index.html
ImVietnam Jun 7, 2023
2b2cbad
Update index.html
ImVietnam Jun 7, 2023
553dfd2
Update index.html
ImVietnam Jun 7, 2023
50750e9
Update solution.md
ImVietnam Jun 7, 2023
38b5f2c
Update task.md
ImVietnam Jun 7, 2023
8c22db5
Update index.html
ImVietnam Jun 7, 2023
ac1334d
Update index.html
ImVietnam Jun 7, 2023
51b6380
Update index.html
ImVietnam Jun 7, 2023
2cac560
Update index.html
ImVietnam Jun 7, 2023
f0c8678
Update article.md
ImVietnam Jun 7, 2023
aaea552
Update article.md
ImVietnam Jun 7, 2023
0a79d79
Merge branch 'javascript-tutorial:master' into patch-4
ImVietnam Jun 9, 2023
e2d9de3
Merge branch 'javascript-tutorial:master' into patch-4
ImVietnam Aug 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
34 changes: 17 additions & 17 deletions 1-js/03-code-quality/05-testing-mocha/3-pow-test-wrong/solution.md
Original file line number Diff line number Diff 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 x to power n", function() {
it("5 in the power of 1 equals 5", function() {
describe("Nâng x lên luỹ thừa n", function() {
it("5 mũ 1 bằng 5", function() {
assert.equal(pow(5, 1), 5);
});

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

it("5 in the power of 3 equals 125", function() {
it("5 mũ 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 x to power n", function() {
it("5 in the power of 1 equals 5", function() {
describe("Nâng x lên luỹ thừa n", function() {
it("5 mũ 1 bằng 5", function() {
assert.equal(pow(5, 1), 5);
});

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

it("5 in the power of 3 equals 125", function() {
it("5 mũ 3 bằng 125", function() {
assert.equal(pow(5, 3), 125);
});
});
Expand Down
Original file line number Diff line number Diff 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 x to the power n", function() {
it("Nâng x lê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 deletions 1-js/03-code-quality/05-testing-mocha/article.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff 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">
<!-- add mocha 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>
// chai has a lot of stuff, let's make assert global
// chai có 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
14 changes: 7 additions & 7 deletions 1-js/03-code-quality/05-testing-mocha/beforeafter.view/test.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
describe("test", function() {

// Mocha usually waits for the tests for 2 seconds before considering them wrong
// Mocha thườ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 to 200,000 milliseconds
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.000 mili 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 deletions 1-js/03-code-quality/05-testing-mocha/index.html
Original file line number Diff line number Diff 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">
<!-- add mocha 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>
// chai has a lot of stuff, let's make assert global
// chai có 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 deletions 1-js/03-code-quality/05-testing-mocha/pow-1.view/index.html
Original file line number Diff line number Diff 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">
<!-- add mocha 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>
// chai has a lot of stuff, let's make assert global
// chai có 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 deletions 1-js/03-code-quality/05-testing-mocha/pow-2.view/index.html
Original file line number Diff line number Diff 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">
<!-- add mocha 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>
// chai has a lot of stuff, let's make assert global
// chai có 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 deletions 1-js/03-code-quality/05-testing-mocha/pow-3.view/index.html
Original file line number Diff line number Diff 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">
<!-- add mocha 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>
// chai has a lot of stuff, let's make assert global
// chai có 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 deletions 1-js/03-code-quality/05-testing-mocha/pow-4.view/index.html
Original file line number Diff line number Diff 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">
<!-- add mocha 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>
// chai has a lot of stuff, let's make assert global
// chai có 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
2 changes: 1 addition & 1 deletion 1-js/03-code-quality/05-testing-mocha/pow-4.view/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ describe("pow", function() {

});

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