@@ -2,16 +2,14 @@ import { expect } from '@esm-bundle/chai';
2
2
import mqtt from '../../' ; // this will resolve to mqtt/dist/mqtt.esm.js
3
3
4
4
// needed to test no-esm version /dist/mqtt.js
5
- /** @type { import('../../src').MqttClient }*/
5
+ /** @type { import('../../src') }*/
6
6
const mqtt2 = window . mqtt
7
7
8
8
// get browser name
9
9
const userAgent = navigator . userAgent . toLowerCase ( ) . replace ( / / g, '_' ) . replace ( / \/ / g, '_' )
10
10
11
11
let browser = 'unknown'
12
12
13
- console . log ( 'userAgent:' , userAgent )
14
-
15
13
if ( userAgent . includes ( 'chrome' ) ) {
16
14
browser = 'chrome'
17
15
} else if ( userAgent . includes ( 'firefox' ) ) {
@@ -21,40 +19,50 @@ if (userAgent.includes('chrome')) {
21
19
}
22
20
23
21
const browserTopic = `test/${ browser } `
22
+ console . log ( 'User Agent:' , userAgent )
23
+ console . log ( 'Browser:' , browser )
24
24
25
- console . log ( 'browser:' , browser )
26
-
27
- function run ( proto , port , cb ) {
28
-
25
+ function testProto ( proto , port , cb = ( ) => { } ) {
29
26
const testTopic = `${ browserTopic } /${ proto } `
30
27
31
28
describe ( 'MQTT.js browser test with ' + proto . toUpperCase ( ) , ( ) => {
32
29
after ( ( ) => {
33
- if ( cb ) {
30
+ if ( client ) {
31
+ client . end ( ( ) => {
32
+ cb ( )
33
+ client = null ;
34
+ } ) ;
35
+ } else {
34
36
cb ( )
35
37
}
36
38
} )
37
39
38
- const client = mqtt . connect ( `${ proto } ://localhost:${ port } ` , {
39
- // log: console.log.bind(console),
40
- } )
41
- client . on ( 'offline' , ( ) => {
42
- console . log ( 'client offline' )
43
- } )
44
- client . on ( 'connect' , ( ) => {
45
- console . log ( 'client connect' )
46
- } )
47
- client . on ( 'reconnect' , ( ) => {
48
- console . log ( 'client reconnect' )
49
- } )
40
+ /** @type { import('../../src').MqttClient }*/
41
+ let client = null ;
50
42
51
43
it ( 'should connect-publish-subscribe' , ( done ) => {
44
+ client = mqtt . connect ( `${ proto } ://localhost:${ port } ` , {
45
+ // log: console.log.bind(console),
46
+ clientId : `testClient-${ browser } -${ proto } ` ,
47
+ } )
48
+ client . on ( 'offline' , ( ) => {
49
+ console . log ( 'client offline' )
50
+ done ( new Error ( 'client offline' ) )
51
+ } )
52
+ client . on ( 'connect' , ( ) => {
53
+ console . log ( 'client connect' )
54
+ } )
55
+ client . on ( 'reconnect' , ( ) => {
56
+ console . log ( 'client reconnect' )
57
+ } )
58
+
52
59
const payload = 'Hello World!'
53
60
client . on ( 'connect' , ( ) => {
54
61
client . on ( 'message' , ( topic , msg ) => {
55
62
expect ( topic ) . to . equal ( testTopic ) ;
56
63
expect ( msg . toString ( ) ) . to . equal ( payload ) ;
57
64
client . end ( ( ) => {
65
+ client = null ;
58
66
done ( ) ;
59
67
} ) ;
60
68
} ) ;
@@ -76,30 +84,32 @@ function run(proto, port, cb) {
76
84
} )
77
85
}
78
86
79
- it ( 'should work with non-ESM version' , ( ) => {
80
- expect ( mqtt2 ) . to . exist
81
- expect ( mqtt2 . connect ) . to . exist
82
- expect ( mqtt2 . connect ) . to . be . a ( 'function' )
83
- } )
87
+ describe ( 'MQTT.js browser tests' , ( ) => {
88
+ it ( 'should work with ESM version' , ( done ) => {
89
+ expect ( mqtt2 ) . to . exist
90
+ expect ( mqtt2 . connect ) . to . be . a ( 'function' )
91
+ expect ( mqtt2 . Client ) . to . be . a ( 'function' )
92
+ done ( )
93
+ } )
84
94
95
+ it ( 'should work in a Web Worker' , ( done ) => {
96
+ const worker = new Worker ( 'test/browser/worker.js' )
97
+ worker . onmessage = ( e ) => {
98
+ if ( e . data === 'worker ready' ) {
99
+ done ( )
100
+ } else {
101
+ done ( Error ( e . data ) )
102
+ }
103
+ }
85
104
86
- run ( 'ws' , window . wsPort , ( ) => {
87
- run ( 'wss' , window . wssPort , ( ) => {
88
- describe ( 'MQTT.js browser test with web worker' , ( ) => {
89
- it ( 'should work with web worker' , async ( ) => {
90
- const worker = new Worker ( 'test/browser/worker.js' )
91
- const ready = new Promise ( ( resolve , reject ) => {
92
- worker . onmessage = ( e ) => {
93
- if ( e . data === 'worker ready' ) {
94
- resolve ( )
95
- } else {
96
- reject ( e . data )
97
- }
98
- }
99
- } )
100
- await ready
101
- } )
102
- } )
105
+ worker . onerror = ( e ) => {
106
+ done ( Error ( e . message ) )
107
+ }
108
+ } )
109
+
110
+ testProto ( 'ws' , window . wsPort , ( ) => {
111
+ testProto ( 'wss' , window . wssPort )
103
112
} )
104
113
} )
105
114
115
+
0 commit comments