-
Notifications
You must be signed in to change notification settings - Fork 22
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add isNodeJs and process getters to cli_pkg/js.dart #143
Merged
Merged
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
0788652
Add 'isNodeJs' getter to cli_pkg/js.dart
Goodwine abd9488
Add the 'process' getter to cli_pkg/js.dart
Goodwine 8344322
Add browser tests, oops didn't realize dart made testing browsers ver…
Goodwine 88fdbe4
use .toJSBox for objects, .toJS is deleted in Dart Dev
Goodwine ea785a0
add example that uses process.env
Goodwine f96010a
ok.. avoid using toJSBox too on globalThis
Goodwine e26df98
disable dart-dev CI testing due to https://github.com/dart-lang/sdk/i…
Goodwine 7ca1ecd
lint?
Goodwine 49ee9da
review
Goodwine 9ece1f9
rename nodejs-in-browser test file
Goodwine 2f42a21
review
Goodwine File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// Copyright 2023 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import 'dart:js_interop'; | ||
import 'dart:js_util'; | ||
|
||
import 'package:cli_pkg/js.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
@TestOn('browser') | ||
Goodwine marked this conversation as resolved.
Show resolved
Hide resolved
|
||
void main() { | ||
tearDown(() => delete<Object>(globalThis, 'process')); | ||
|
||
const nonNodeJsProcessTestCases = <String, Map<String, Map<String, String>>>{ | ||
'an empty process': {}, | ||
'a process with empty release': {'release': {}}, | ||
'a process with non-Node.JS release name': { | ||
'release': {'name': 'hello'} | ||
}, | ||
}; | ||
|
||
const fakeNodeJsProcess = { | ||
'release': {'name': 'node'} | ||
}; | ||
|
||
group('isNodeJs', () { | ||
test("returns 'false' from the browser", () { | ||
expect(isNodeJs, isFalse); | ||
}); | ||
|
||
for (final entry in nonNodeJsProcessTestCases.entries) { | ||
final caseName = entry.key; | ||
final processJson = entry.value.jsify(); | ||
|
||
test("returns 'false' when $caseName exists in the 'window'", () { | ||
setProperty(globalThis, 'process', processJson); | ||
expect(isNodeJs, isFalse); | ||
}); | ||
} | ||
|
||
test("returns 'true' with a fake Node.JS process", () { | ||
setProperty(globalThis, 'process', fakeNodeJsProcess.jsify()); | ||
expect(isNodeJs, isTrue); | ||
}); | ||
}); | ||
|
||
group('process', () { | ||
test("returns 'null' from the browser", () { | ||
expect(process, isNull); | ||
}); | ||
|
||
for (final entry in nonNodeJsProcessTestCases.entries) { | ||
final caseName = entry.key; | ||
final processJson = entry.value.jsify(); | ||
|
||
test("returns 'null' when $caseName exists in the 'window'", () { | ||
setProperty(globalThis, 'process', processJson); | ||
expect(process, isNull); | ||
}); | ||
} | ||
|
||
test("returns a fake process if it fakes being a Node.JS environment", () { | ||
setProperty(globalThis, 'process', fakeNodeJsProcess.jsify()); | ||
expect(process.jsify().dartify(), fakeNodeJsProcess); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this should be in
browser_library_test
, since it's not running in a browser and it's not loading a JS library exposed by the package. It would probably make more sense to put it intest/node_js_test.dart
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is actually running in a browser 🤔, just without any other libraries involved. There are tests for running within Node already. I will rename this file to make it less confusing.
I guess it's a good idea to expand
browser_test_shared.dart
to verify none of those libraries would incorrectly returnisNodeJs == true